Ambiguity/private inheritance scope [C++]
Greetings, I currently have some code in which almost every class needs to privately inherit a global parent class, and I've hit a problem when class A inherits the global parent, class B likewise inherits that global parent, and class B inherits class A; perhaps code will make more sense...
I thought that this wouldn't pose a problem, as the GlobalParent inherited by Parent should be hidden from/inaccessible to Child; except that I've obviously misunderstood the scope rules in C++, and Child is seeing both the GlobalParent it inherits, and the GlobalParent inherited by Parent, which should be hidden/private...
Any suggestions as to how to fix this?
Code:
class GlobalParent
{
public:
void Say( void ) { printf( "sealfin" ); return; };
};
class Parent : private GlobalParent
{
};
class Child : private GlobalParent, private Parent
{
};g++ Wrote:warning: direct base `GlobalParent' inaccessible in `Child' due to ambiguity
I thought that this wouldn't pose a problem, as the GlobalParent inherited by Parent should be hidden from/inaccessible to Child; except that I've obviously misunderstood the scope rules in C++, and Child is seeing both the GlobalParent it inherits, and the GlobalParent inherited by Parent, which should be hidden/private...
Any suggestions as to how to fix this?
Mark Bishop
You need to use virtual inheritance, eg "class Child : public virtual Parent"
But, looking at your example code, it seems like you'd be better of rethinking the class hierarchy.
But, looking at your example code, it seems like you'd be better of rethinking the class hierarchy.
DoG Wrote:You need to use virtual inheritance
Thanks for the suggestion DoG!
I need a better book on C++
That does still seem like a bug in the inheritance scope rules though, given that the inheritance was private 
DoG Wrote:But [...] it seems like you'd be better of rethinking the class hierarchyUnfortunately, that isn't possible
Mark Bishop
sealfin Wrote:Thanks for the suggestion DoG!I need a better book on C++
That does still seem like a bug in the inheritance scope rules though, given that the inheritance was private
Unfortunately, that isn't possible
[noparse]When you inherit from a class as private, that doesn't mean its inaccessible. It just affects the scopes of the parent class in the inherited class, eg Parent::public becomes a part of Child::private.[/noparse]
Also, why is it not possible to refactor this? Your inheritance scheme seems pretty nonsensical.
I've never used anything other than public inheritance... chances are you're doing something wacky
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Virtual functions/Inheritance. | BinarySpike | 5 | 3,811 |
Sep 29, 2005 11:16 AM Last Post: BinarySpike |
|

