注册 登录
编程论坛 C++教室

C++PrimerP576问题请教

企鹅 发布于 2008-08-28 03:04, 637 次点击
#include <iostream>
using namespace std;
class Base
{
friend class Frnd;
protected:
int i;
};

// Frnd has no access to members in D1
class D1 : public Base
{
public:
D1(){i=0;}
protected:
int j;
};

class Frnd
{
public:
int mem(Base b){return b.i;}//ok:Frnd is friend to Base
int mem(D1 d){return d.i;}//error:friendship doesn't inherit
};

// D2 has no access to members in Base
class D2:public Frnd
{
public:
// int mem(Base b) {return b.i;}//error:friendship doesn't inherit
};

int main()
{
D1 test;
Frnd fnd;
cout<<fnd.mem(test);
return 0;
}


书上说友元关系不能继承。为什么fnd可以访问D1的变量呢?
0 回复
1