Ambiguity/private inheritance scope [C++]

Moderator
Posts: 702
Joined: 2002.04
Post: #1
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...

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
--
Student and freelance OS X & iOS developer
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #3
DoG Wrote:You need to use virtual inheritance

Thanks for the suggestion DoG! Grin I need a better book on C++ Annoyed That does still seem like a bug in the inheritance scope rules though, given that the inheritance was private Annoyed

DoG Wrote:But [...] it seems like you'd be better of rethinking the class hierarchy
Unfortunately, that isn't possible Annoyed

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
sealfin Wrote:Thanks for the suggestion DoG! Grin I need a better book on C++ Annoyed That does still seem like a bug in the inheritance scope rules though, given that the inheritance was private Annoyed

Unfortunately, that isn't possible Annoyed

[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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
I've never used anything other than public inheritance... chances are you're doing something wacky Rasp
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Virtual functions/Inheritance. BinarySpike 5 4,265 Sep 29, 2005 11:16 AM
Last Post: BinarySpike