您的位置:首页 > 编程语言 > C语言/C++

c++学习笔记之友元

2014-09-22 22:47 260 查看
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Test
{
public:
friend class Other; //声明Other是Test的朋友
friend void bar(const Test &t);
private:
int x_;
int y_;
};

class Other
{
public:
void foo(Test &t)
{
t.x_ = 10;
t.y_ = 20;
}
};

void bar(const Test &t)
{
cout << t.x_ << endl;
}

int main(int argc, const char *argv[])
{
Test t;
return 0;
}

函数可以声明为类的友元,另一个类也可以声明为友元

友元 声明为public private都可以
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: