Private/protected inheritance problem

M

Mark A. Gibbs

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.

--
Mark A. Gibbs (aka. Indi)
Administrator
#c++ on irc.Rizon.net

http://ca.geocities.com/[email protected]/
(temporary website)
 
H

Howard Hinnant

Mark A. Gibbs said:
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
 
S

Sep

Couldn't you also just use a reinterpret_cast to cast this to a bar *?
If so, this would get around having to making foo<bar> a friend of bar.
Please correct me if this is not an optimal idea.
 
M

Mark A. Gibbs

*that's* the conceptual roadblock i couldn't get past. thank you. now
the error makes sense.
Couldn't you also just use a reinterpret_cast to cast this to a bar *?
If so, this would get around having to making foo<bar> a friend of bar.
Please correct me if this is not an optimal idea.

as is, that's not a big issue. if you want public inheritance you don't
need it, if you want private (or protected inheritance), it's not that
big a deal. i've also created two constructors for foo, one that
calculates the address of bar as i showed, and one that accepts a bar*
if you don't want to make foo a friend of bar (partial template
instantiation makes this work).

still, if that would work, and is safe and portable, i'd use it. does
anyone know?

--
Mark A. Gibbs (aka. Indi)
Administrator
#c++ on irc.Rizon.net

http://ca.geocities.com/[email protected]/
(temporary website)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top