Re: Private/protected inheritance problem

From: Howard Hinnant (hinnant_at_metrowerks.com)
Date: 03/12/05


Date: Sat, 12 Mar 2005 03:43:36 GMT

In article <f_ednbr955Uc_a_fRVn-iA@rogers.com>,
 "Mark A. Gibbs" <x_gibbsmark@rogers.com_x> wrote:

> Good day,
>
> i'm having a bit of trouble with a base class i'm working on. this is
> what it boils down to:
>
> template <typename T>
> class foo
> {
> protected:
> foo() { T* p = static_cast<T*>(this); }
> };
>
> class bar : foo<bar>
> {
> public:
> bar() {}
> };
>
> as is bar inherits privately from foo, and i get a compile error on the
> assignment saying "Illegal access from bar to private/protected member
> foo::" (that's exactly what it says, just "foo::").
>
> when i use public inheritance, there's no problem. however, i get the
> same problem for protected inheritance, or when i make foo::foo() a
> public constructor.
>
> is there a reason for this? i mean, when foo::foo() is instantiated, the
> relationship between foo and T is known and clear. i don't see what
> private member of foo bar is trying to access. i tried making a
> workaround by doing this:
>
> int distance = static_cast<foo<T>*>((T*)0);
> T* p = reinterpret_cast<T*>(this - distance);
>
> but it still failed on the first line.
>
> i'm using codewarrior 8. what i want to do is get a pointer to the
> derived class from the base class.

The fact that a foo<bar>* can be converted to a bar* is privileged
information. Afterall it is just an implementation detail that a bar is
implemented in terms of a foo<bar>. Most (non-derived) bar clients
should be ignorant of that fact.

You could try:

class bar : foo<bar>
{
    friend class foo<bar>;
public:
    bar() {}
};

This gives foo<bar> the access rights it needs to make that pointer
conversion.

-Howard



Relevant Pages

  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (sci.electronics.design)
  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (comp.arch.embedded)
  • Re: "Mastering C Pointers"....
    ... > Windows shortcut, there is definitely an analogy there. ... a pointer variable holds an address of an object. ... int foo = 12; ... int *bar; ...
    (comp.lang.c)
  • Re: Interview questions
    ... and note that this means the implementor of Bar needs to ... Bar might want to inherit some virtual methods of Foo. ... is the first member of bar, we can say a Bar "is a" Foo. ... > You cannot pass a pointer to an object of type Bar ...
    (comp.lang.cpp)
  • Difficulty Extending from Generic Base Class
    ... Bar extend from BaseClass): ... public class Foo: BaseClass ... public override string ToString() ... The list of abstract base class items is a concrete class. ...
    (microsoft.public.dotnet.framework.clr)