????friend关键字
2016-03-24 13:35
267 查看
#include "stdafx.h" #include <iostream> using namespace std; //A里面有个private的变量,需要被其他的如 //全局函数g(A *a)、h()、结构体C、结构体中的某个函数B::f(A *a)访问 struct A; struct B{ void f(A *a); //这里用到了A,那么前面必须前面有A的声明 }; struct A{ private: int i; public: void ini(); friend void g(A *a); //在这里声明也是算数的,算是全局的声明,因此g和h可以写在下面 friend void h(); friend struct C; //这个表明在C的任何一个函数内都可以访问到 friend void B::f(A *a); //这里必须事先有该函数声明才行,因此B的结构体定义前移 }; void B::f(A *a){ cout<<"B::f gets the value: "<<a->i<<endl; } void A::ini(){ i=20; } void g(A *a){ cout<<"g gets the value: "<<a->i<<endl; } void h(){ A a; a.i=10; cout<<"can new an A in h: "<<a.i<<endl; } struct C{ }; int _tmain(int argc, _TCHAR* argv[]) { return 0; }
相关文章推荐
- AsyncHttpClient, RequestParams ,AsyncHttpResponseHandler三个类使用方法
- 大数据方面的顶级开源工具
- 毕业设计之甘特图制作
- MooseFS源代码分析(三)
- 加载com.mysql.jdbc.Driver失败 jdbc.properties连接信息多了个空格
- IOS 刷机教程
- Android开发学习笔记:数据存取之SQLite浅析
- 微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题
- Java中的equals学习小结
- tomcat单机多实例部署
- Makefile VPATH和vpath的使用
- 讲题
- 地理位置坐标转换
- Dungeon Master(bfs)
- django环境搭建
- 【POJ】1321 - 棋盘问题(dfs)
- 微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题
- 如何计算Eden区的大小
- ExtJs之addManagedListener
- mysql 系列