一个种的友元类能访问类的私有数据

一个类的友元类能访问类的私有数据// test.cpp : Defines the entry point for the console application./

一个类的友元类能访问类的私有数据

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class CAnimal
{
public :
 int m_sex;
public:
 void SetSex(int sex)
 {
  m_sex = sex;
 }
 //******************************
 //定义友元函数
 friend int GetSex(CAnimal& am);

 ///********************************
 //声明友类
 friend class CCat;
};

int GetSex(CAnimal& am)
{
 return am.m_sex ;
}

class CCat
{
public:
 int GetSex(CAnimal& am)
 {
  return am.m_sex ;
 }
};


int main(int argc, char* argv[])
{
 CAnimal animal;
 CCat  kitty;
 animal.m_sex = 'M';

 int sex =kitty.GetSex(animal);
 return 0;
}