C++ friend function and friend class
来源:互联网 发布:冯满天知乎 编辑:程序博客网 时间:2024/05/22 07:03
Friends are not member functions. a friend function of a class is defined outside that class but it has the right to access all private and protected members of the class.Even though the prototype for friend functions appear in the class definition,friends are not member functions.
a friend can be a function,template function ,or member function,or a class or class template,in which case the entire class and all of its members are friends.
To declare a function as a friend of a class, precede the prototype in the class definition with keyword friend as follows:
class A{ int x;public: friend int getsValue(A& a);};// friend functionint getsValue(A &a){return a.x;}To declare all members of the class ClassOne as friends of class ClassOne ,place a following declaration in the definition of the ClassOne.
friend class ClassOne;consider the following program:
#include<iostream>using namespace std;class Test{public:Test(int a):x(a){};friend int getsValue(Test &classA);private:int x;};int getsValue(Test &classA){return classA.x;}int main(int argc,char *argv[]){Test A(10);cout<<"value in class Test is : "<<getsValue(A)<<endl;return 0;}when the above code is complied and excuted ,it produces the following result:
value in class Test is : 10
阅读全文
0 0
- C++ friend function and friend class
- [C++] C++中friend的使用(friend function and friend class)
- C++中friend的使用(friend function and friend class)
- C++中friend的使用(friend function and friend class) .
- C++中friend的使用(friend function and friend class)
- C++中friend的使用(friend function and friend class)
- C++中friend的使用(friend function and friend class)
- C++中friend的使用(friend function and friend class)
- friend class & function
- Friend class or function and nested types in class templates
- [C++] friend class
- C++::Class Friend
- friend function
- friend class
- friend class
- friend class
- [C++]friend
- friend
- get请求
- SIFT特征提取分析
- MFC 资源视图 对话框中控件 无法添加变量 或者双击左键之后没法跳转到对应函数
- 【简记】大规模Web开发技术(第五章)
- 6 个技巧,提升 C++11 的 vector 性能
- C++ friend function and friend class
- 深入分析Java中的length和length()
- Linux学习笔记2
- Tomcate部署乱码解决方案
- Guice依赖注入的三种方式和injectMembers方法的使用
- win7 x64位 redis安装与使用
- Linux上Tomcat启用软链接的三种方式 【转】
- 字符设备实例,根据《linux设备驱动开发详解》globalmem驱动加上个人理解的注释
- EOJ----章鱼哥没有日历