一 友元函数的定义与调用
C++类除了成员函数,静态成员函数,还有一种函数:友元函数。
友元函数和类一种朋友关系,它不属于类,但可以访问类的私有成员,它和类是一种朋友关系,也不需要用类的对象去驱动,例如下面的代码:
#include
using namespace std;
class ASD{
friend int getData(const ASD& asd);
private:
int m_Value;
public:
ASD(int value = 0):m_Value(value){}
~ASD(){}
};
int getData(const ASD& asd)
{
return asd.m_Value;
}
int main() {
ASD obj(123);
cout
关注
打赏