4.4.2类做友元
#include <iostream>
using namespace std;
#include<string>
class building;
class good
{
public:
good()
{
}
void visit();
building *building;
};
class building
{
friend class good;
public:
building();
public:
string m_sittingroom;
private:
string m_bedroom;
};
building::building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
void good::visit()
{
cout << building->m_sittingroom << endl;
}
void test01()
{
good gg;
gg.visit();
}
int main4_4——2()
{
test01();
system("pause");
return 0;
}
4.4.1全局函数做友元
#include <iostream>
using namespace std;
#include <string>
class building
{
friend void good(building *building);
public:
building()
{
m_sittingroom = "客厅";
m_badroom = "卧室";
}
public:
string m_sittingroom;
private:
string m_badroom;
};
void good(building *building)
{
cout << building->m_sittingroom << endl;
cout << building->m_badroom << endl;
}
int main4_4_1()
{
building b;
good(&b);
system("pause");
return 0;
}