C++ friend function and friend class
2017-05-22 15:19
483 查看
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:
#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
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 function int 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
相关文章推荐
- C++中friend的使用(friend function and friend class)
- C++中friend的使用(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++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】
- C++ and Java template class and function 模板类和模板函数
- Friend class or function and nested types in class templates
- [C++] Pure Virtual Function and Abstract Class
- Member Function Pointers and the Fastest Possible C++ Delegates
- Friend function should be a public function, if it is in a class.
- [C++]Partly Specialize member function in Class Templates
- Why C++ Supports both Class and Typename for Type Parameters
- C++ String class Examples and Tutorial
- virtual function and abstract class
- Fast C++ Delegate: Boost.Function 'drop-in' replacement and multicast
- C++: member function pointer for SomeClass
- Reading Notes: Chapter 1. Policy-Based Class Design(Modern C++ Design By Andrei Alexandrescu)
- C++ use Templates(function && class)