iDevGames Forums
Ambiguity/private inheritance scope [C++] - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Ambiguity/private inheritance scope [C++] (/thread-3597.html)



Ambiguity/private inheritance scope [C++] - sealfin - Dec 29, 2006 06:09 AM

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?


Ambiguity/private inheritance scope [C++] - DoG - Dec 29, 2006 08:08 AM

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.


Ambiguity/private inheritance scope [C++] - sealfin - Dec 29, 2006 10:08 AM

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


Ambiguity/private inheritance scope [C++] - DoG - Dec 29, 2006 10:40 AM

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.


Ambiguity/private inheritance scope [C++] - OneSadCookie - Dec 29, 2006 02:27 PM

I've never used anything other than public inheritance... chances are you're doing something wacky Rasp