Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Polymorphism doesn't work with base class value member?
QtWS25 Last Chance

Polymorphism doesn't work with base class value member?

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 1 Posters 392 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on last edited by enjoysmath
    #1

    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"?

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    1 Reply Last reply
    0
    • enjoysmathE Offline
      enjoysmathE Offline
      enjoysmath
      wrote on last edited by enjoysmath
      #2

      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.

      https://github.com/enjoysmath
      https://math.stackexchange.com/users/26327/exercisingmathematician

      1 Reply Last reply
      0
      • enjoysmathE Offline
        enjoysmathE Offline
        enjoysmath
        wrote on last edited by
        #3

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

        https://github.com/enjoysmath
        https://math.stackexchange.com/users/26327/exercisingmathematician

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved