#include<iostream>
#include<ctime>
using namespace std;
class Person;
class goodFriend
{
public:
Person * p ;
//goodFriend()
//{
// p = new Person;
//}
void visitIngoodFriend();
};
class Person
{
//声明友元类
friend class goodFriend;
public:
Person();
string P_name;
private:
string P_sex;
};
Person::Person()
{
this->P_name = "xxx";
this->P_sex = "man";
}
//goodFriend::goodFriend()
//{
// this->p = new Person;
//}
//void test02() //错误 不能在类外直接写函数访问
//{
// goodFriend f1;
// //
// cout<<f1.p->P_name << f1.p->P_sex;
//
//}
void goodFriend::visitIngoodFriend()
{
p = new Person();
cout << p->P_name << " " << p->P_sex << endl;
delete p;
}
void test02()
{
goodFriend f1;
f1.visitIngoodFriend();
}
int main()
{
test02();
return 0;
}
C++友元类
最新推荐文章于 2024-11-09 20:38:27 发布