Newb Multiple Virtual Inheritance Help

H

Heinz Ketchup

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!
 
S

Salt_Peter

Heinz said:
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.
 
H

Heinz Ketchup

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top