T
t
Lippman's C++ Primer, 4th ed., p575 says:
Friendship is not inherited. Friends of the base class have no
special access to members of its derived class. If a base class is
granted friendship, only the base has special access. Classes derived
from that base have no access to the class granting friendship.
I tried this in Visual C++ 2005 Express, and it seems to only
implement half of the friendship rules above.
Example code:
=================================================
class A2;
class A
{
friend class B;
friend void g(A, A2);
private:
int x;
};
class A2 : public A
{
private:
int x2;
};
class B
{
public:
void f(A a) { a.x; }
void f2(A2 a2) { a2.x; a2.x2;}
// a2.x compiles, but a2.x2 gives compile error
// both statements in f2 should be compile errors ???
};
class B2 : public B
{
public:
// void f(A a) { a.x; } // gives compile error, as it should
};
void g(A a, A2 a2)
{
a.x; // compiles
a2.x; // compiles, but shouldn't ?
a2.x2; // doesn't compile
}
int main()
{
}
=================================================
Is Visual C++ 2005 Express not implementing friendship rules correctly?
Friendship is not inherited. Friends of the base class have no
special access to members of its derived class. If a base class is
granted friendship, only the base has special access. Classes derived
from that base have no access to the class granting friendship.
I tried this in Visual C++ 2005 Express, and it seems to only
implement half of the friendship rules above.
Example code:
=================================================
class A2;
class A
{
friend class B;
friend void g(A, A2);
private:
int x;
};
class A2 : public A
{
private:
int x2;
};
class B
{
public:
void f(A a) { a.x; }
void f2(A2 a2) { a2.x; a2.x2;}
// a2.x compiles, but a2.x2 gives compile error
// both statements in f2 should be compile errors ???
};
class B2 : public B
{
public:
// void f(A a) { a.x; } // gives compile error, as it should
};
void g(A a, A2 a2)
{
a.x; // compiles
a2.x; // compiles, but shouldn't ?
a2.x2; // doesn't compile
}
int main()
{
}
=================================================
Is Visual C++ 2005 Express not implementing friendship rules correctly?