Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Inhertance Diamond problem

Inhertance Diamond problem

Scheduled Pinned Locked Moved C / C++ / MFC
oophelp
6 Posts 4 Posters 0 Views 1 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.
  • L Offline
    L Offline
    laksh2204
    wrote on last edited by
    #1

    Hi, I have a very famous problem with multiple inheritance here. class A{ public: A(){ cout<<"A"<

    N _ S 3 Replies Last reply
    0
    • L laksh2204

      Hi, I have a very famous problem with multiple inheritance here. class A{ public: A(){ cout<<"A"<

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      laksh2204 wrote:

      I am not able to understand that why the constructor of A is called again after B. Or why not the same is happening after constructor of C is called.

      So the compiler goes likes this... First time it sees "new D" it 1. calls constructor of D 2. which in turn calls constructor of B (Since B if the first base class) 3. which in turn calls constructor of A (Prints "A") 4. then (Prints "B") 5. then the compiler calls constructor of C (Second base class) 6. which in turn again calls constructor of A (Prints "A") 7. then (Prints "C") 8. in the end (Prints "D") So bottom line is unless base class constructor code executes derived class constructor doesn't execute.

      Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

      modified on Wednesday, August 27, 2008 8:58 AM

      L 1 Reply Last reply
      0
      • L laksh2204

        Hi, I have a very famous problem with multiple inheritance here. class A{ public: A(){ cout<<"A"<

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        This is due to the fact that the base class constructor has to get called before the derived class constructor gets call. since D derives from both B and C constructor of A gets called in either of the cases

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        1 Reply Last reply
        0
        • N Nibu babu thomas

          laksh2204 wrote:

          I am not able to understand that why the constructor of A is called again after B. Or why not the same is happening after constructor of C is called.

          So the compiler goes likes this... First time it sees "new D" it 1. calls constructor of D 2. which in turn calls constructor of B (Since B if the first base class) 3. which in turn calls constructor of A (Prints "A") 4. then (Prints "B") 5. then the compiler calls constructor of C (Second base class) 6. which in turn again calls constructor of A (Prints "A") 7. then (Prints "C") 8. in the end (Prints "D") So bottom line is unless base class constructor code executes derived class constructor doesn't execute.

          Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

          modified on Wednesday, August 27, 2008 8:58 AM

          L Offline
          L Offline
          laksh2204
          wrote on last edited by
          #4

          OK, thats nice explanation.. one more issue comes into my mind. A / \ B C \ / D In the same diamond structure if my declaration is like: class A; class B : virtual public A; class C : virtual public A; class D : public B, public C; now when i do A *d = new D; //it will work as ambiguity is removed by virtual. But, which path will be followed?? A->B->D or A->C->D?? or something else...

          N 1 Reply Last reply
          0
          • L laksh2204

            OK, thats nice explanation.. one more issue comes into my mind. A / \ B C \ / D In the same diamond structure if my declaration is like: class A; class B : virtual public A; class C : virtual public A; class D : public B, public C; now when i do A *d = new D; //it will work as ambiguity is removed by virtual. But, which path will be followed?? A->B->D or A->C->D?? or something else...

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            laksh2204 wrote:

            A *d = new D; //it will work as ambiguity is removed by virtual. But, which path will be followed?? A->B->D or A->C->D??

            Now your output will be A B C D I guess first base class gets the preference.

            Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            1 Reply Last reply
            0
            • L laksh2204

              Hi, I have a very famous problem with multiple inheritance here. class A{ public: A(){ cout<<"A"<

              S Offline
              S Offline
              Sarath C
              wrote on last edited by
              #6

              In Addition to Nibu's comment, it's better to approach diamond inheritance in terms of Virtual Inheritance. Diamond Problem[^] Virtual Inheritance [^] Virtual Inheritance- C++ FAQ[^]

              -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

              My blog - Sharing My Thoughts, An Article - Understanding Statepattern

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups