Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > In accessible base class for return pointer type

Reply
Thread Tools

In accessible base class for return pointer type

 
 
Steven T. Hatton
Guest
Posts: n/a
 
      03-11-2006
Can somebody explain why making T a friend of either B or C will permit the
code to compile?

class A{ protected: A(){} };
class T;
class B: protected A {protected: B(){}/*friend class T;*/};
class C: public B {public : C(){}/*friend class T;*/};
struct T { A* newA() {return new C();}};
int main() {}

This is the error I get if I do not use one of the friend declarations:
In member function ?A* T::newA()?: error: ?A? is an inaccessible base of ?C?

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-11-2006
Steven T. Hatton wrote:
> Can somebody explain why making T a friend of either B or C will
> permit the code to compile?
>
> class A{ protected: A(){} };
> class T;
> class B: protected A {protected: B(){}/*friend class T;*/};
> class C: public B {public : C(){}/*friend class T;*/};
> struct T { A* newA() {return new C();}};
> int main() {}
>
> This is the error I get if I do not use one of the friend
> declarations:
> In member function ?A* T::newA()?: error: ?A? is an inaccessible base
> of ?C?


Conversions from a pointer to derived to a pointer base require the
base to be accessible. If 'A' is a protected base of 'C' (through
'B'), then no outside class/function can convert a pointer to 'C' to
a pointer to 'A'. Since 'B' is a public base of 'C', anyone can
convert a 'C*' to a 'B*'.

What book on C++ are you reading that doesn't explain access specifiers
and their effect?


V
--
Please remove capital As from my address when replying by mail


 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      03-11-2006
Victor Bazarov wrote:

> Steven T. Hatton wrote:
>> Can somebody explain why making T a friend of either B or C will
>> permit the code to compile?
>>
>> class A{ protected: A(){} };
>> class T;
>> class B: protected A {protected: B(){}/*friend class T;*/};
>> class C: public B {public : C(){}/*friend class T;*/};
>> struct T { A* newA() {return new C();}};
>> int main() {}
>>
>> This is the error I get if I do not use one of the friend
>> declarations:
>> In member function ?A* T::newA()?: error: ?A? is an inaccessible base
>> of ?C?

>
> Conversions from a pointer to derived to a pointer base require the
> base to be accessible. If 'A' is a protected base of 'C' (through
> 'B'), then no outside class/function can convert a pointer to 'C' to
> a pointer to 'A'. Since 'B' is a public base of 'C', anyone can
> convert a 'C*' to a 'B*'.
>
> What book on C++ are you reading that doesn't explain access specifiers
> and their effect?
>
>
> V


I guess I could/should have looked it up. I just wanted to see what others
had to say about it. After your suggestion that I access a reference, I
did take a look at TC++PL(SE) 15.3.2. Basically what it says, and what
you said are the supposition I drew from the example. Part of what was
unclear in my mind is what scope the conversion takes place in. The
original code was calling the counterpart of C::newA (above) from within
the counterpart of A. It took some time to untangle what was happening
because I had never encountered that particular situation, that I can
remember.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting derived class pointer to private base class pointer subramanian100in@yahoo.com, India C++ 8 08-18-2010 10:54 AM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Copy constructor for a class that contains a pointer to a base class type (newbie) SzH C++ 1 10-27-2006 09:21 PM
Pointer to class equals pointer to base class Alberto Luaces C++ 6 06-19-2006 06:16 PM
[RTTI] cast base class pointer to <templated> derived class pointer tirath C++ 3 10-12-2003 01:44 PM



Advertisments