473,402 Members | 2,053 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,402 software developers and data experts.

In accessible base class for return pointer type

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/
Mar 11 '06 #1
2 4398
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
Mar 11 '06 #2
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/
Mar 11 '06 #3

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

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
7
by: James Brown | last post by:
I have two classes, a base class, and a class base { public: base(); virtual void foo() = 0; }; class derived : public base
8
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
14
by: mlimber | last post by:
In an article on the safe bool idiom (http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives the following code (slightly modified): class safe_bool_base { protected: typedef...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
13
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.