友元函数
类friendOfA可访问类RoadPersonA的私有成员iAge。(虽然提供了访问其他类私有成员的方法,但是破坏了数据安全性,慎用。)
class RoadPersonA
{
public:
RoadPersonA()
{
}
~RoadPersonA()
{
}
friend class friendOfA;
private:
int iAge = 21;
};
class friendOfA
{
public:
int GetAgeOfroadPersonA(RoadPersonA roadPersonA)
{
return roadPersonA.iAge;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
RoadPersonA roadPersonA;
friendOfA friendOfA;
cout << friendOfA.GetAgeOfroadPersonA(roadPersonA) << endl;
return 0;
}