473,396 Members | 1,914 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.

Access to protected base member

Given the following code:
class A
{
protected:
int pro;
};

class B : public A
{
public:
void foo()
{
A * a;
a->pro;

B * b;
b->pro2;
}
protected:
int pro2;
};

The line b->pro2 compiles fine, however the line a->pro does not compile.
Could someone please explain to me why.
Thanks.
Jul 22 '05 #1
7 1923
On Thu, 17 Jun 2004 12:11:48 +1200 in comp.lang.c++, "Andy Ward"
<an*******@ihug.co.nz> wrote,
The line b->pro2 compiles fine, however the line a->pro does not compile.
Could someone please explain to me why.


Pointer b is promised to point to a B instance, therefore it is
legitimate for class B members to access protected members.

Pointer a can point to A and other derived classes that are not B
instances, therefore it would be wrong for B to have access.

Jul 22 '05 #2
"David Harmon" <so****@netcom.com.invalid> wrote in message
news:40****************@news.west.earthlink.net...
On Thu, 17 Jun 2004 12:11:48 +1200 in comp.lang.c++, "Andy Ward"
<an*******@ihug.co.nz> wrote,
The line b->pro2 compiles fine, however the line a->pro does not compile.
Could someone please explain to me why.


Pointer b is promised to point to a B instance, therefore it is
legitimate for class B members to access protected members.

Pointer a can point to A and other derived classes that are not B
instances, therefore it would be wrong for B to have access.


Of course, thank you.
Jul 22 '05 #3
David Harmon wrote:

Pointer a can point to A and other derived classes that are not B
instances, therefore it would be wrong for B to have access.


class B: public A { ... };

public derivation means that base class( A ) protected members can be
used by members and friends of derived class( B ). How does above not
break this rule?

thanks - hunter
Jul 22 '05 #4
On Fri, 18 Jun 2004 16:51:22 +0800 in comp.lang.c++, Hunter Hou
<hy***@lucent.com> wrote,
David Harmon wrote:

Pointer a can point to A and other derived classes that are not B
instances, therefore it would be wrong for B to have access.


class B: public A { ... };

public derivation means that base class( A ) protected members can be
used by members and friends of derived class( B ). How does above not
break this rule?


public|protected|private derivation mean that protected members of A can
be accessed from B when the A is actually the base class part of a B
object. You do not get freebie access to mess around with the base
class members of C objects, D objects, E objects or any other class that
is foreign to B just because it also derives from A.

I hope that is more clear; I don't know how else to say it.

Jul 22 '05 #5
David Harmon wrote:
On Fri, 18 Jun 2004 16:51:22 +0800 in comp.lang.c++, Hunter Hou
<hy***@lucent.com> wrote,
David Harmon wrote:

Pointer a can point to A and other derived classes that are not B
instances, therefore it would be wrong for B to have access.


class B: public A { ... };

public derivation means that base class( A ) protected members can be
used by members and friends of derived class( B ). How does above not
break this rule?

public|protected|private derivation mean that protected members of A can
be accessed from B when the A is actually the base class part of a B
object. You do not get freebie access to mess around with the base
class members of C objects, D objects, E objects or any other class that
is foreign to B just because it also derives from A.

I hope that is more clear; I don't know how else to say it.


ok. I understand your meaning, but I think that's not convincing. In
class A, I change protected to public, then everything is fine. No
errors any longer.

Jul 22 '05 #6
* Andy Ward:
Given the following code:
class A
{
protected:
int pro;
};

class B : public A
{
public:
void foo()
{
A * a;
a->pro;

B * b;
b->pro2;
}
protected:
int pro2;
};

The line b->pro2 compiles fine, however the line a->pro does not compile.
Could someone please explain to me why.


A B-object can only access protected A-stuff in the A-part of B-objects.

'protected' isn't a carte blanche to go messing around inside objects.

It only makes the _definitions_ available, "known", to derived classes,
and if you then create an object of such a derived class, say B, you
(the class B code) can access all that you know in your own object.

On the other hand there's no stopping the determined programmer from
doing evil things.

And sometimes that's necessary (e.g. getting access to the container
inside a std::list).
#include <iostream>

class A
{
protected:
int myValue;
public:
A( int aValue ): myValue( aValue ) {}
};

struct Hack: A
{
static int value( A const* p )
{
return static_cast<Hack const*>( p )->myValue;
}
};

class B: public A
{
public:
static int f()
{
A a( 1234 );

// return a.myValue; -- does not compile.
return Hack::value( &a ); // Oooh, dirty!
}
};

int main()
{
std::cout << B::f() << std::endl; // "1234"
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #7
Hunter Hou wrote:


ok. I understand your meaning, but I think that's not convincing. In
class A, I change protected to public, then everything is fine. No
errors any longer.


please ignore this meesage.
Jul 22 '05 #8

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

Similar topics

2
by: Michael Young | last post by:
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...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
1
by: vDave420 | last post by:
I feel stupid, because I can't figure out why this shouldn't work. I am getting this error using MS VS6 sp5 i believe. Is this correct? ----------------- namespace Test { class Base
4
by: Siemel Naran | last post by:
Can Derived class static member access protected member from base class? class Base { protected: void setvariable(int); }; class Derived : Base { public: static std::auto_ptr<Base> out(new...
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...
3
by: Nathan Wiegman | last post by:
Hi, Why must derived classes access protected base members through an instance of a derived class? The following code would work in C++, why doesn't it in C#? public class A { protected...
6
by: Roka | last post by:
Hi all why cann't I access protected var from a inherited class? Code example: class Base{ protected: int color; public: virtual void show() = 0; };
3
by: Samuel Burri | last post by:
Hi there I got a simple problem. As you can see in the posted source, I have two classes, where one is derived from the other. In fact, they also contain virtual functions. In the function...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
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
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
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
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
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.