protected member

R

REH

Can someone please tell me what is wrong with this snippet of code?
GCC is telling me that "foo2 is protected within this context" (that
I cannot use it with BBB).


class AAA {
protected:
virtual int foo2() {return 0;}
};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}
};
 
A

antonov84

Can someone please tell me what is wrong with this snippet of code?
GCC is telling me that "foo2 is protected within this context" (that
I cannot use it with BBB).

class AAA {
protected:
virtual int foo2() {return 0;}

};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}};

BBB has access to AAA::foo2 only as a part of itself, i.e. you can do
this->AAA::foo2() inside BBB::foo, but you cant do it on a separate
object. You can however befirend BBB in AAA, and thus gain access to
separate objects too. Also, if you're certain the dynamic type of your
'a' parameter is BBB, you can do "static_cast<BBB&>(a).foo2();" , this
would probably compile to non-virtual foo2 call though.
 
A

antonov84

BBB has access to AAA::foo2 only as a part of itself, i.e. you can do
this->AAA::foo2() inside BBB::foo, but you cant do it on a separate
object. You can however befirend BBB in AAA, and thus gain access to
separate objects too. Also, if you're certain the dynamic type of your
'a' parameter is BBB, you can do "static_cast<BBB&>(a).foo2();" , this
would probably compile to non-virtual foo2 call though.

when you see 'separate object' in this post, read it as a 'separate
object of type AAA' of course ;]
 
S

Salt_Peter

Can someone please tell me what is wrong with this snippet of code?
GCC is telling me that "foo2 is protected within this context" (that
I cannot use it with BBB).

class AAA {
protected:
virtual int foo2() {return 0;}

};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}

};

The member function int BBB::foo(AAA&) is attempting to access the
interface provided by the parameter's type, not that instance of BBB.
the parameter AAA& a is *not* the base entity so only the public
interface is available.

Why should the program give you access to a part of the object you
didn't provide access to?
 
A

Alf P. Steinbach

* REH:
Can someone please tell me what is wrong with this snippet of code?
GCC is telling me that "foo2 is protected within this context" (that
I cannot use it with BBB).


class AAA {
protected:
virtual int foo2() {return 0;}
};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}
};

Consider

class Foo
{
protected:
void doDangerousStuff() {}
Foo() {}
public:
void doSafeStuff() {}
};

class UsefulFoo: public Foo
{
public:
UsefulFoo() {}
};

struct FooHacked: Foo
{
void doThatDangerousStuff( Foo& o )
{ o.doDangerousStuff(); }
};

int main()
{
UsefulFoo o;
FooHacked::doThatDangerousStuff( o );
}

If C++ allowed this, then "protected" wouldn't yield much protection,
now would it? You could then easily, inadvertently, do something like
FooHacked. I'm not sure whether this is in the FAQ or not (if not it
should be there, it's certainly frequently asked about): a quick
skimming of the FAQ didn't find it.

Of course you can override the protection by casting, but typically that
invokes formally Undefined Behavior.
 
M

Markus Schoder

BBB has access to AAA::foo2 only as a part of itself, i.e. you can do
this->AAA::foo2() inside BBB::foo, but you cant do it on a separate
object. You can however befirend BBB in AAA, and thus gain access to
separate objects too.

Member access is based on type not object identity. Access to AAA::foo2()
is allowed for a separate object but only through a BBB reference (or
pointer).
Also, if you're certain the dynamic type of your
'a' parameter is BBB, you can do "static_cast<BBB&>(a).foo2();" , this
would probably compile to non-virtual foo2 call though.

The call is still virtual and "static_cast<BBB&>(a).AAA::foo2();" is also
valid.
 

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
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top