By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

Access to protected base member

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
* 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.