* 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.
--
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?