Heinz Ketchup wrote:
Quote:
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
Quote:
>
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.
Quote:
>
Please don't shoot me, still learning here.
|
The base classes need to be declared first
Quote:
>
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...
Quote:
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.
Quote:
>
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!
|