标题:C++PrimerP576问题请教
只看楼主
企鹅
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-7-14
 问题点数:0 回复次数:0 
C++PrimerP576问题请教
#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的变量呢?
搜索更多相关主题的帖子: friendship include members access public 
2008-08-28 03:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-230195-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.068924 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved