dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Class Inheritance Question

  1. #1
    Join Date
    Jan 2009
    Posts
    16

    Class Inheritance Question

    Hey there,

    Just a quick question about class inheritance:

    If I have a class C that inherits from both class A and class B.

    Class A and class B have a virtual function called foo()

    To avoid issues in class C I would put this as the declaration as C::foo()

    Code:
    virtual void C::foo(){ A::foo(); }
    To make sure it ran A's foo() instead of B's

    Is this the correct way to do things? Or should I declare B's foo() without the virtual directive? Even if A's data members are declared as protected and are easily accessible in class C. Should I leave them both as virtual in class A and B and just copy class A's foo() into class C's foo()?

    I guess what I'm asking is what normally happens in this situation.

    Thanks,
    Lang

  2. #2
    Join Date
    Mar 2001
    Posts
    2,527

    Re: Class Inheritance Question

    This is fine:
    Code:
    virtual void C::foo(){ A::foo(); }
    You are being explicit which is a correct solution.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Mar 2001
    Posts
    2,527

    Re: Class Inheritance Question

    Do A and B have a common base class? An Abstract base?

    Problem #1 If not, you may want to test your code to make sure that the correct underlying member variables are being manipulated.

    Have a look at virtual public inheritance. You will see the inheritance diamond and see the way that having virtual public inheritance when using multiple inheritance makes sure that problem #1 does not occur. Virtual public inheritance also may cause some performance hit.
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Class Inheritance Question

    If I have a class C that inherits from both class A and class B.
    Multiple inheritance with the same function name in both classes is asking for trouble.

    Code:
    class C : public A, public B
    {
       foo ();
    };
    
    C myclass;
    
    myclass.foo ();
    Which one is called ?

  5. #5
    Join Date
    Mar 2001
    Posts
    2,527

    Re: Class Inheritance Question

    well if A::foo() and B::foo() are both virtual then they are meant to be overridden. We need to see more of this guys code to know exactly how to respond, or more about his class structure.

    so if C::foo() explicitly calls A::foo() this is fine, as long as the rest of his class structure supports it.
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    Join Date
    Mar 2001
    Posts
    2,527

    Re: Class Inheritance Question

    Lang please post your class code!

    Thank You!
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)