Polymorphism doesn't work with base class value member?



  • I have:

    class B {
         virtual string toString() { return "B";}
    }
    
    class A : public B
    {
          string toString() { return "A"; }
    }
    
    class C {
       C(A a) {
            b = a;
       }
        B b;
        string toString() {
             return b.toString();
        }
    }
    
    C c(A());
    c.toString();    // yields "B" always.
    

    Is there anyway to keep my current method of doing most things by-value (members especially) without using pointers, and get the above to polymorphically toString() to "A"?



  • Found this:

    https://github.com/jbcoe/polymorphic_value

    I'm assuming I just download the header file into the same folder as my headers, include and use the class.

    Seems like it should work. If it doesn't, I'll mark this as "unsolved."

    UPDATE:

    This doesn't work with my current MinGW kit, I will try to get other kits to work before I emulate the missing C++14 headers.



  • Solution is to install MSVC 2017 for Qt5 kit, which I think has polymorphic_value capabilities now.


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.