472,949 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,949 software developers and data experts.

Newb Multiple Virtual Inheritance Help

Hello,

I'm looking to bounce ideas off of anyone, since mainly the idea of using
Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack
of C++ Experience.

Here is my scenario...

I have 5 Derived Classes
I have 3 Base Classes

The relationship between Base / Derived is more along the lines of Has-A,
rather than Is-A

Please don't shoot me, still learning here.

Derived Classes:
Class A : private AAA, private BBB
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Different
virtual void 4_Methods() (); // Do it My Way Different
}
Class B : private AAA, private BBB
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Slightly Different
virtual void 4_Methods() (); // Do it My Way Slightly Different
}
Class C : private AAA, private BBB
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Really Different
virtual void 4_Methods() (); // Do it My Way Really Different
}
Class D : private AAA, private CCC
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Kinda Different
virtual void 3_Methods() (); // Do it My Way Kinda Different
}
Class E : private AAA, private CCC
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Plain Different
virtual void 3_Methods() (); // Do it My Way Plain Different
}

Base Classes:
Class AAA :
{
protected:
void 17_Methods();
virtual void 7_Methods() = 0; // Implementation I need from Derived
}
Class BBB :
{
void 13_Methods();
virtual void 4_Methods() = 0; // Implementation I need from Derived
}
Class CCC :
{
void 6_Methods();
virtual void 3_Methods() = 0; // Implementation I need from Derived
}

The Reasoning behind this Nightmare:
I have 3 Base Classes primarily with Common Implementation Code.
The UNCOMMON code in the base class is declared to be pure virtuals, so
that when the Base Class makes that particular method call, it will be
over-ridden by the Derived Class (which has the correct implmentation in
that class).

In addition to this problem:
Class BBB and Class CCC, rely on values from Class AAA (That is, the
Derived Class will be passing on values into Class BBB / CCC methods, with
the exception of the Virtual Methods)

I'm sure there is a far better solution, there always is...
and I've thought about Aggregation, but I'm unsure as to how I can get the
same data relationship when I do it the inheritance way.

i.e. Base Pure Virtual Call -Goes to Derived for Implementation.

If anyone can point me in an alternate suggestion, maybe templates instead,
or something... + explanation that'd be great too.

Thanks for all / any help.
It's much Appreciated,
Heinz!


Oct 17 '06 #1
2 1775
Heinz Ketchup wrote:
Hello,

I'm looking to bounce ideas off of anyone, since mainly the idea of using
Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack
of C++ Experience.
private inheritance is really a form of composition, see the FAQ
>
Here is my scenario...

I have 5 Derived Classes
I have 3 Base Classes

The relationship between Base / Derived is more along the lines of Has-A,
rather than Is-A
Yes, so no problem since private inheritence means Derived in_terms_of
Base.
A car in_terms_of its serial number or license plate.
>
Please don't shoot me, still learning here.
The base classes need to be declared first
>
Derived Classes:
Class A : private AAA, private BBB
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Different
virtual void 4_Methods() (); // Do it My Way Different
}
semicolon please!

Since you later mention that class BBB and CCC are dependant on AAA:
why not only make AAA an abstract class? Since AAA's ctor is not
guarenteed to be invoked before BBB's ctor in the code above (its
implementation defined - not guarenteed). Try only privately deriving
from abstract class AAA. Let the others (BBB/CCC) be plain members of
the class.

That way, you'll have all your derivatives in_terms_of class AAA and
the members can be initialized using private AAA's pertinant data.
thats a hint, by the way - see below.

Also, using access specifier *protected* on pure virtuals is usually
not a good idea since that means you'll never be allowed to override
the virtual methods if you derive from class A. As an example: a
protected pure virtual *needs_not* be reimplemented in
DerivedA : public A { ... };
since class A already implements the *required* virtual member
function.
Surprised? Does that not raise a whole new question?
Imagine if a client really does need to overide the protected pure
virtual's implementation?
He'ld have to derive from AAA again - bad news. Showstopper.
Anyways:

Class AAA // abstract
{
public:
void 17_Methods();
virtual void 7_Methods() = 0; // pure-virtual
};

Class BBB // not abstract, can be a member
{
public:
void 13_Methods();
void 4_Methods() ;
};

Class A : private AAA
{
BBB bbb;
public:
A() : bbb() { } // AAA's def ctor is invoked first - that is
guarenteed and a hint
// so BBB's ctor can use AAA's data - another
hint
// hint: where is AAA's def ctor? it may be
critical here
void MySay() {};
void 7_Methods() {}; // automatically virtual
void 4_Methods() { bbb.4_Methods; }
};

etc...
Class B : private AAA, private BBB
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Slightly Different
virtual void 4_Methods() (); // Do it My Way Slightly Different
}
Class C : private AAA, private BBB
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Really Different
virtual void 4_Methods() (); // Do it My Way Really Different
}
Class D : private AAA, private CCC
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Kinda Different
virtual void 3_Methods() (); // Do it My Way Kinda Different
}
Class E : private AAA, private CCC
{
public:
void MySay() {};
protected:
virtual void 7_Methods() {}; // Do it My Way Plain Different
virtual void 3_Methods() (); // Do it My Way Plain Different
}

Base Classes:
Class AAA :
{
protected:
void 17_Methods();
virtual void 7_Methods() = 0; // Implementation I need from Derived
}
Class BBB :
{
void 13_Methods();
virtual void 4_Methods() = 0; // Implementation I need from Derived
}
Class CCC :
{
void 6_Methods();
virtual void 3_Methods() = 0; // Implementation I need from Derived
}

The Reasoning behind this Nightmare:
I have 3 Base Classes primarily with Common Implementation Code.
The UNCOMMON code in the base class is declared to be pure virtuals, so
that when the Base Class makes that particular method call, it will be
over-ridden by the Derived Class (which has the correct implmentation in
that class).
Not neccessarily, a pure virtual member function must be implemented
but not neccessarily at the "most" Derived class.
>
In addition to this problem:
Class BBB and Class CCC, rely on values from Class AAA (That is, the
Derived Class will be passing on values into Class BBB / CCC methods, with
the exception of the Virtual Methods)

I'm sure there is a far better solution, there always is...
and I've thought about Aggregation, but I'm unsure as to how I can get the
same data relationship when I do it the inheritance way.

i.e. Base Pure Virtual Call -Goes to Derived for Implementation.

If anyone can point me in an alternate suggestion, maybe templates instead,
or something... + explanation that'd be great too.

Thanks for all / any help.
It's much Appreciated,
Heinz!
Oct 17 '06 #2
Hello Salt_Peter,

Thanks for pointing me in the right direction! I read over all your comments
and they are fantastic!

I never really considered anyone inheriting from my derived classes, since
I'm the only person who's working on this portion of the project.

Of course from a maintenance issue with another programmer, I'm sure they'd
be unhappy. My thinking was along the lines of having Protected Constructors
/ Methods; and that way, I could guarantee that nobody could try and create
objects out of my base classes. {Generating a Compiler Error with a Protected
Constructor}

Someone using the classes would have to know how everything worked.

Oh yes, thanks for the *hints*, in trying to nudge my brain into a little bit
more thought on order of operations during derivation / construction /
destruction.
I wish there were more people like you that I could ask around at work, but
sadly... there are none.

Thanks again! You've been a great help!

Best Regards,
Heinz

Oct 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
8
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
11
by: axel22 | last post by:
Please observe this simple model of multiple inheritance: void main() { class A { public: virtual void print() { cout << "A" << endl; }; class Support1 : virtual public A {
3
by: ernesto | last post by:
Hi everybody I have the following class declarations: class Interface { public: virtual char* getName() const = 0; }; class BaseClass : public Interface {
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.