CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2002
    Posts
    31

    Multiple Inheritance

    I have seven classes C1,C2,C3,C4,C5,C6,C7 such that:

    1) C2,C3 are derived from C1
    2) C4 is derived from C2 and C5 is derived from C3
    3) C6 is derived from C2 and C3 (Multiple)
    4) C7 is derived from C4, C5 and C6 (Multiple)

    Here the question is for which classes I have to derive virtually so that I can aceess a function defined in C1 in C7 with out conflicts.

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    This is just the beginning of your problems, but you want one non-virtual path the the base class. So, try and make C2 or C3 inherit virtually.

    Jeff
    Last edited by jfaust; July 23rd, 2002 at 10:06 AM.

  3. #3
    Join Date
    Jan 2001
    Posts
    588
    That sounds like one heck of a family tree. You may want to rethink your design before you try to get hard into any implementation. If there is any better way to do it, I would recommend going in that direction.

  4. #4
    Join Date
    May 2002
    Posts
    31
    jfaust ,

    I have tried that also. The problems with function conflict is not there at this time, some constructor problem is coming. something saying that C1::C1() has problem while compiling the file containg the C7 class (Each class is in separate files and all of them are in one project).


    Chandu

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Chandu,

    Actually, I think you want to make both C2 and C3 virtual.

    Jeff

  6. #6
    Join Date
    May 2002
    Posts
    31
    Yes,
    I have done this

    class C2: virtual public C1

    class C3: virtual public C1

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Give it up now. Do not go down this route.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Graham's completely correct. If this is new development, don't do it. Find a better solution. I guarantee there is one.

    These types of hierarchies were much more common in the days when templates were not as well supported as they are now. All-encomposing hierarchies were used so that you could create a container class that maintained pointers to a common base class. Think CObject in MFC.

    My point is that if what you are working on already has this hierarchy, it may be too expensive to fix at this point, even if it should be fixed.

    Jeff

  9. #9
    Join Date
    May 2002
    Posts
    31
    I am doing this because, I have to go this way only, this classes are generated by some lexical analyzer which compiles files of some format and generate C++ classes for me. So I can make only minor changes to generated files. I can't redesign the class architecture because the tool which generates code is some 3rd partys.

    -Chandu

  10. #10
    Join Date
    Oct 2001
    Posts
    42
    If I understood you correct, you want to have a single instance of C1 base class in the structure of C7?
    Yes, it can be achieved by virtually inheriting both C2 and C3 from C1.
    But it's not enough. You have to change initialization of C1 as well.
    Virtual base class is initialized by the mostly derived one, i.e. by C7 in your case.
    Does C1 have a default constructor?
    If not, it must be explicitly initialized in C7 constructor's member initialization list using whatever constructor C1 has.

    Nikolai

  11. #11
    Join Date
    Jun 2002
    Posts
    224
    The code below works fine. You said something about a C1 default constructor related error. Could you please post the error message(s)?
    Code:
    #include <iostream>
    
    using namespace std;
    
    class C1
    { public: void boo() { cout << "C1::boo" << endl; } };
    
    class C2 : virtual public C1
    {};
    
    class C3 : virtual public C1
    {};
    
    class C4 : public C2
    {};
    
    class C5 : public C3
    {};
    
    class C6 : public C2, public C3
    {};
    
    class C7 : public C4, public C5
    {};
    
    int main()
    {
      C7().boo(); 
      return 0;
    }
    Regards,
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  12. #12
    Join Date
    Oct 2001
    Posts
    42
    In your example neither of your classes have
    use-defined constructors and, therefore,
    implicit default constructors are in effect.
    I think these implicit default constructors look like this:

    C1(){};
    C2():C1(){};
    C3():C1(){};
    C4():C1(),C2(){};
    C5():C1(),C3(){};
    C6():C1(),C2(),C3(){};
    C7():C1(),C4(),C5(){};

    Note that every derived constructor first initializes virtual base class C1.
    Actual initialization happens only once in the most derived constructor
    (depending on of whatever class the object is declared).
    Any other initializations of C1 are ignored.

    Things become different when virtual base class C1 contains a non-default constructor.
    In this case C1 has no longer a default constructor and MUST be
    explicitly initialized in the most derived class.

    Here is an example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class C1
    { public: C1(int i) {}; // non-default constructor
              void boo() { cout << "C1::boo" << endl; }
    };
    
    class C2 : virtual public C1
    { public: C2():C1(2){}; };
    
    class C3 : virtual public C1
    { public: C3():C1(3){}; };
    
    class C4 : public C2
    { public: C4():C1(4){}; };
    
    class C5 : public C3
    { public: C5():C1(5){}; };
    
    class C6 : public C2, public C3
    {};
    
    class C7 : public C4, public C5
    { public: C7():C1(7){}; };
    
    int main()
    {
      C7().boo(); 
      return 0;
    }
    Class C7 initializes virtual subobject C1 explicitely now.
    Note that C6 does not have that explicit initialization and it compiles OK.
    The reason is that no object of C6 is declared.
    When object is declared compiler generates an error about C6 class.

    Also I discovered that Comeau C++ compiler requires
    explicit initialization of C1 in every subobject(C2,C3,C4,C5), even though
    when C7 object is declared they are ignored.

    Nikolai

  13. #13
    Join Date
    Jun 2002
    Posts
    224
    Nikolai,
    My question was, actually, for chanduonline:
    some constructor problem is coming. something saying that C1::C1() has problem while compiling the file containg the C7 class
    Sorry!
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  14. #14
    Join Date
    May 2002
    Posts
    31
    Nikolai Borissov,

    Thanks for your detailed explanation. I will try doing that. I don't have a default constructor in any of the classes(All need some initialisation).

    zdf,

    My error message is
    " no match for call: C1* -> C1::C1() "

    As 'Nikolai Borissov' said,

    I think this because C7 is trying to intialise the C1 constructor with default constructor. But there is no default constructor in C1 that is why it might be giving error. I will try initilising C1 explicitly in C7 constructor.

    -Chandu

  15. #15
    Join Date
    May 2002
    Posts
    31
    Thanks friends.
    The problem got solved by initializing the C1 constructor in C7.


    -Chandu

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured