473,809 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_cas t<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 1953
In article <f_************ ********@rogers .com>,
"Mark A. Gibbs" <x_*********@ro gers.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_cas t<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_cas t 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_cas t 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
6434
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 std::endl;
3
2945
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 define destructor as "private/protected"? Thanks!
2
2523
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 'class base *' exists, but is inaccessible" If I inherit using public it works well ... I am not very clear about the private inheritance
1
448
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 having private inheritance then when you have public inheritance. I just want to have you opinion about this matter.
7
3536
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
7832
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 scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
6
4648
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 'private' or 'protected'? I've looked through some tutorials on inheritance, but the ones I found are mostly concerned with covering the basic idea and simply skip over this and take it for granted, so I'm hoping one of you can tell me.
10
4812
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 pure virtual function body
3
2167
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 method may do the same thing. My embedded compiler and g++ disagree on whether this is legal. Example: class Base { public: void foo(); }; class Derived : private Base { public: void foo(); };
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10115
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.