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. Virtual inheritance polymorphic call resolution
Forum Updated to NodeBB v4.3 + New Features

Virtual inheritance polymorphic call resolution

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 3 Posters 2.3k Views 3 Watching
  • 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.
  • K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 29 Apr 2017, 12:21 last edited by kshegunov
    #1

    Hello,
    This happens so rarely that I seem to have forgotten this and that. Suppose I have a diamond inheritance (virtual inheritance for base classes is provided), such as:

    class A
    {
    public:
       virtual void method();
    };
    
    class B : public virtual A
    {
    public:
        void method() override;
    };
    
    class C : public virtual A
    {
    };
    
    class D : public B, public C
    {
    };
    

    I was wondering what happens if one calls a virtual function from the last derived class with the base pointer? As far as I can tell the call should be ambiguous, so am I required to provide an override in the last class of the hierarchy to resolve it? From the example above something along the lines:

    class D : public B, public C
    {
    public:
        void method() override
        {
            B::method();
        }
    };
    

    Read and abide by the Qt Code of Conduct

    M 1 Reply Last reply 29 Apr 2017, 12:39
    0
    • K kshegunov
      29 Apr 2017, 12:21

      Hello,
      This happens so rarely that I seem to have forgotten this and that. Suppose I have a diamond inheritance (virtual inheritance for base classes is provided), such as:

      class A
      {
      public:
         virtual void method();
      };
      
      class B : public virtual A
      {
      public:
          void method() override;
      };
      
      class C : public virtual A
      {
      };
      
      class D : public B, public C
      {
      };
      

      I was wondering what happens if one calls a virtual function from the last derived class with the base pointer? As far as I can tell the call should be ambiguous, so am I required to provide an override in the last class of the hierarchy to resolve it? From the example above something along the lines:

      class D : public B, public C
      {
      public:
          void method() override
          {
              B::method();
          }
      };
      
      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Apr 2017, 12:39 last edited by mrjj
      #2

      @kshegunov
      Hi
      It seems spot on.
      My memory was even more vague about the diamond case so i consulted
      http://www.cprogramming.com/tutorial/virtual_inheritance.html
      and yes using virtual should be the solution.
      But let's see if there are other opinions or solutions.
      Being in C++ Gurus, ill just keep low profile ;)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 29 Apr 2017, 15:00 last edited by
        #3

        Ah yes, the famous problem deemed unsolvable by Java creators, and yet solved and working in c++ decades ago ;)

        @kshegunov Draw it on a piece of paper. There's no diamond here. Just a triangle with a dangling A outside of it ;) Having that, you've got your virtuals wrong. The classes that inherit the same base (B) are C and D, so that's where you need the virtual keyword:

        class A
        {
        public:
           virtual void method();
        };
        
        class B : public A
        {
        public:
            void method() override;
        };
        
        class C : public virtual B
        {
        };
        
        class D : public virtual B, public C
        {
        public:
            void method() override
            {
                B::method();
            }
        };
        

        In the example you posted B::method() would be indeed ambiguous and as such - is invalid and won't compile.

        K 1 Reply Last reply 29 Apr 2017, 15:09
        0
        • C Chris Kawa
          29 Apr 2017, 15:00

          Ah yes, the famous problem deemed unsolvable by Java creators, and yet solved and working in c++ decades ago ;)

          @kshegunov Draw it on a piece of paper. There's no diamond here. Just a triangle with a dangling A outside of it ;) Having that, you've got your virtuals wrong. The classes that inherit the same base (B) are C and D, so that's where you need the virtual keyword:

          class A
          {
          public:
             virtual void method();
          };
          
          class B : public A
          {
          public:
              void method() override;
          };
          
          class C : public virtual B
          {
          };
          
          class D : public virtual B, public C
          {
          public:
              void method() override
              {
                  B::method();
              }
          };
          

          In the example you posted B::method() would be indeed ambiguous and as such - is invalid and won't compile.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 29 Apr 2017, 15:09 last edited by
          #4

          There's a typo, Chris, C should derive from A and it is a diamond.
          Read as:

          class C : public virtual A
          

          PS. Corrected in the original post.

          Read and abide by the Qt Code of Conduct

          C 1 Reply Last reply 29 Apr 2017, 15:10
          0
          • K kshegunov
            29 Apr 2017, 15:09

            There's a typo, Chris, C should derive from A and it is a diamond.
            Read as:

            class C : public virtual A
            

            PS. Corrected in the original post.

            C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 29 Apr 2017, 15:10 last edited by
            #5

            Oh, ok, then your original solution was indeed correct.

            K 1 Reply Last reply 29 Apr 2017, 15:15
            3
            • C Chris Kawa
              29 Apr 2017, 15:10

              Oh, ok, then your original solution was indeed correct.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 29 Apr 2017, 15:15 last edited by
              #6

              Okay. Thanks!

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              1/6

              29 Apr 2017, 12:21

              • Login

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