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

Question about base class member access

I've been trying to compile code similar to the example code below,
but I keep getting errors indicating that the function 'foo()' is not
accessible. At first, I thought this was a bug in the compiler, but
I've now tried the code against Borland C++ Builder 6.0, MS VC++ .NET,
DevC++ (GCC), and Comeau's on-line compiler, and all give me similar
diagnositic messages.

I can make the code work if I make the "protected" items "public" in
the base class. But that's really not what I want...

So, what is wrong with my code? I was under the impression that any
member of a class that is publicly derived from a base class should be
able to access the base class public and protected members.
Obviously, I've oversimplified it and missed something along the way
(or maybe it doesn't apply here for some reason?).

If anyone can quote chapter and verse from the standard (preferably
with a somewhat more "human" explanation as well) that explains the
behavior, I'd greatly appreciate it!

#include <cstdlib>
//---------------------------------------------------------------------------
class Base
{
// public :
protected :

Base ( Base* structural_child )
: polymorphic_child_( structural_child )
{
}

virtual
void
foo ( unsigned int recurse_count ) const = 0 ;

// public :
protected :

Base* polymorphic_child_ ; // In "real" code, there is a list
// of pointers to objects, and this
// class is a hierarchical data
// structure.
} ;
//---------------------------------------------------------------------------
class Derived
: public Base
{
public :

Derived ( )
: Base( NULL )
{
}

protected :

virtual
void
foo ( unsigned int recurse_count ) const
{
if ( ( NULL != polymorphic_child_ ) &&
( recurse_count > 0 ) ) {
// Problem arises from following line
polymorphic_child_->foo( --recurse_count ) ;
}
}
} ;
//---------------------------------------------------------------------------
int
main ( int argc, char* argv[] )
{
Derived d ;
return( 0 ) ;
}
Jul 19 '05 #1
2 1934

"Michael Young" <mi***********@paetec.com> wrote in message
news:83**************************@posting.google.c om...
I've been trying to compile code similar to the example code below,
but I keep getting errors indicating that the function 'foo()' is not
accessible. At first, I thought this was a bug in the compiler, but
I've now tried the code against Borland C++ Builder 6.0, MS VC++ .NET,
DevC++ (GCC), and Comeau's on-line compiler, and all give me similar
diagnositic messages.

I can make the code work if I make the "protected" items "public" in
the base class. But that's really not what I want...

So, what is wrong with my code?
It breaks the access rules of C++.
I was under the impression that any
member of a class that is publicly derived from a base class should be
able to access the base class public and protected members.
Obviously, I've oversimplified it and missed something along the way
(or maybe it doesn't apply here for some reason?).
Its a very common misunderstanding. If you searched for protected in this
group you would find many other similar questions to yours.

If anyone can quote chapter and verse from the standard (preferably
with a somewhat more "human" explanation as well) that explains the
behavior, I'd greatly appreciate it!

#include <cstdlib>
//--------------------------------------------------------------------------
- class Base
{
// public :
protected :

Base ( Base* structural_child )
: polymorphic_child_( structural_child )
{
}

virtual
void
foo ( unsigned int recurse_count ) const = 0 ;

// public :
protected :

Base* polymorphic_child_ ; // In "real" code, there is a list
// of pointers to objects, and this
// class is a hierarchical data
// structure.
} ;
//--------------------------------------------------------------------------
- class Derived
: public Base
{
public :

Derived ( )
: Base( NULL )
{
}

protected :

virtual
void
foo ( unsigned int recurse_count ) const
{
if ( ( NULL != polymorphic_child_ ) &&
( recurse_count > 0 ) ) {
// Problem arises from following line
polymorphic_child_->foo( --recurse_count ) ;
}
}
} ;
//--------------------------------------------------------------------------
- int
main ( int argc, char* argv[] )
{
Derived d ;
return( 0 ) ;
}


I don't know about the standard but here is Stroustrup.

"A derived class can access a base class' protected members only for objects
of its own type".

In your case the type is Derived but the pointer you are trying to access
via has type Base. Base is not of type Derived. If you declared
polymorphic_child_ as type Derived* (or any type derived from Derived) your
example would work.

Why this rule? I don't honestly know. I've seen it explained with an example
and it made sense at the time but it didn't stick in my mind. Stroustrup
just says cryptically "This prevents subtle errors that would otherwise
occur when one derived class corrupts data belonging to other derived
classes.".

Maybe try a search through the archives of this group for a better
explanation (or maybe someone reading this will oblige).

John
Jul 19 '05 #2
>
Maybe try a search through the archives of this group for a better
explanation (or maybe someone reading this will oblige).


For instance

http://groups.google.co.uk/groups?th...1.dejanews.com

John
Jul 19 '05 #3

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

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
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: wASP | last post by:
Hello everyone, I'm new to C# and ASP.NET, so pardon my stupidity on this one. I'm having a problem with referencing methods/functions external to a class member function. My code is as...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
6
by: tshad | last post by:
I am playing with Inheritance and want to make sure I understand it. I have the following Classes: ******************************************* Public Class AuthHeader:Inherits SoapHeader Public...
4
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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.