473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1817
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.