|
|
Le 27/11/2010 21:27, Warp nous fit lire :
> Le_Forgeron <jgr### [at] freefr> wrote:
>> (2) is not allowed because a() of A is protected, so can only be called
>> by the object itself (in a gross simplification).
I did write "in a gross simplification", didn't I ?
> Needless to say, gcc (and probably most other C++ compilers) will give
> an error for that, so it just cannot be done. If you need to call the
> protected section of the object, take a pointer of the derived type,
> because that's what it has to be.
>
Or "just" make B a friend of A. Or in fact, only the needed function of
B can be a friend of A.
Now, I would question the design any way for such function b
(as it is bypassing the objects' boundaries), but sometimes it's simpler
than redesign everything.
class B;
class A
{
protected:
void a() {}
friend void B::b(); // more restricted than "friend B;"
};
class B : public A
{
protected:
void b(A* other)
{
this->a(); // (1)
other->a(); // (2)
}
};
Post a reply to this message
|
|