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.4k 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.
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on 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

    mrjjM 1 Reply Last reply
    0
    • kshegunovK kshegunov

      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();
          }
      };
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 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.

        kshegunovK 1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          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.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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

          Chris KawaC 1 Reply Last reply
          0
          • kshegunovK kshegunov

            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.

            Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            kshegunovK 1 Reply Last reply
            3
            • Chris KawaC Chris Kawa

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

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              Okay. Thanks!

              Read and abide by the Qt Code of Conduct

              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