Re: Private/protected inheritance problem
From: Howard Hinnant (hinnant_at_metrowerks.com)
Date: 03/12/05
- Next message: MSR: "Copy Constructor and other questions"
- Previous message: Mark A. Gibbs: "Private/protected inheritance problem"
- In reply to: Mark A. Gibbs: "Private/protected inheritance problem"
- Next in thread: Sep: "Re: Private/protected inheritance problem"
- Reply: Sep: "Re: Private/protected inheritance problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: MSR: "Copy Constructor and other questions"
- Previous message: Mark A. Gibbs: "Private/protected inheritance problem"
- In reply to: Mark A. Gibbs: "Private/protected inheritance problem"
- Next in thread: Sep: "Re: Private/protected inheritance problem"
- Reply: Sep: "Re: Private/protected inheritance problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|