473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Private/protected inheritance problem


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/in***@rogers.com/
(temporary website)

Jul 23 '05 #1
3 1927
In article <f_********************@rogers.com>,
"Mark A. Gibbs" <x_*********@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
Jul 23 '05 #2
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.

Jul 23 '05 #3
> > 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.

*that's* the conceptual roadblock i couldn't get past. thank you. now
the error makes sense.

Sep wrote:
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/in***@rogers.com/
(temporary website)

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
3
by: seesaw | last post by:
Compared to "public", how does "private/protected" in inheritance change relationship between classes what is the purpose to define constructor as "private/protected"? is there any usage to...
2
by: MJ | last post by:
Hi I have a following sample code class base and class derived. I have inherited the base class as private and tried to compile the code its giving an error "conversion from 'class derived *' to...
1
by: Tony Johansson | last post by:
Hello! Private inheritance is sometimes called implementation inheritance. If you use this private inheritance how is with the usage of overriding then. Is overriding used less often when...
7
by: spam | last post by:
The following code does not compile: class X {}; class Y : private X {}; class Z : public Y { public: Z(X&) {} // problem here
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: Jordi | last post by:
I was wondering, in C++ the syntax for inheritance is basically this: class Descendant : public Parent { .... }; Can someone tell me what would happen when I would replace 'public' by...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
3
by: jared.grubb | last post by:
Can a private Base class method convert to/from a Derived* using static_cast? The CPL book says that a Derived method is allowed to convert to/from Base, but says nothing about whether a Base...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.