Chinaunix首页 | 论坛 | 博客
  • 博客访问: 537219
  • 博文数量: 146
  • 博客积分: 5030
  • 博客等级: 大校
  • 技术积分: 1820
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-16 20:57
文章分类

全部博文(146)

文章存档

2011年(1)

2010年(4)

2009年(30)

2008年(111)

我的朋友

分类: C/C++

2008-07-14 11:35:33

需求呢是Fri这个类的一个方法print要访问Base类的私有成员x
Fri类放在Base前面,在Fri类前面做个Base类声明
然后呢在Base中做一个Fri成员print的友元声明
注意:这个print的定义不能放在Fri中,必须放在外面,放在Base后面。
唉,c++的语法太负责了,这个小破功能居然拖了我这么长时间。

#include
class Base;
class Fri
{
public:
        int print(Base &b);
};

class Base
{
friend int hello(Base &);
//friend class Fri;
public:
friend int Fri::print(Base &b);
public:
        void    setX(int a)
        {
                x=a;
        }
private:
        int x;
};
int Fri::print(Base &b)
{
        std::cout<<"class member"<}
int hello(Base & a)
{
        std::cout<<"ok"<}
int main()
{
        Base b;
        b.setX(1);
        hello(b);
        Fri f;
        f.print(b);
        return 0;
}

阅读(1188) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~