Hi C++ guys,
I tried to use the friend keyword, and I found out a limitation in using it that is not really justified :
Look at the classes below :
class A //foo class
{
private:
A_routine ();
friend class B;
}
class B; //another foo class
class B1 : public B
{
private:
A a;
public:
B1_routine ()
{
a.A_routine ();//this leads to a compilation error
}
};
class B2 : public B;
...
class Bn : public B;
In this basic example, although B1, B2, ... inherit from B, B1, B2, ... are not considered as friends of class A.
I do not understand why the designers of C++ make this choice, if every heirs of B need to be friends of class A, it forces the programmer to declare all of them as friends of class A. It then makes class A aware of all the class hierarchy of B, which breaks the rule that a class should not have knowledge of all the heirs of another class.
The compiler I used is the one of VC++ 6.0, but I think the problem is the same for the other.
Does someone know the reason behind that?
--
Globos
I tried to use the friend keyword, and I found out a limitation in using it that is not really justified :
Look at the classes below :
class A //foo class
{
private:
A_routine ();
friend class B;
}
class B; //another foo class
class B1 : public B
{
private:
A a;
public:
B1_routine ()
{
a.A_routine ();//this leads to a compilation error
}
};
class B2 : public B;
...
class Bn : public B;
In this basic example, although B1, B2, ... inherit from B, B1, B2, ... are not considered as friends of class A.
I do not understand why the designers of C++ make this choice, if every heirs of B need to be friends of class A, it forces the programmer to declare all of them as friends of class A. It then makes class A aware of all the class hierarchy of B, which breaks the rule that a class should not have knowledge of all the heirs of another class.
The compiler I used is the one of VC++ 6.0, but I think the problem is the same for the other.
Does someone know the reason behind that?
--
Globos