Access to protected base member

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.
Thanks.
 
D

David Harmon

On Thu, 17 Jun 2004 12:11:48 +1200 in comp.lang.c++, "Andy Ward"
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.
 
A

Andy Ward

David Harmon said:
On Thu, 17 Jun 2004 12:11:48 +1200 in comp.lang.c++, "Andy Ward"


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.
 
H

Hunter Hou

David said:
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
 
D

David Harmon

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?[/QUOTE]

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.
 
H

Hunter Hou

David said:
On Fri, 18 Jun 2004 16:51:22 +0800 in comp.lang.c++, Hunter Hou



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.
 
A

Alf P. Steinbach

* 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"
}
 
H

Hunter Hou

Hunter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top