Class and object-friend-1, global function as friend

Class and object-friends

In the program, some private properties also want special functions or classes outside the class to access, so you need to use the technology of
friends. The purpose of friends is to allow a function or class to access
the key to private members and friends in another class. The word is
the three realizations of friend
1. Global function as friend
2. Class as friend
3. Member function as friend

1. Global functions as friends

#include<iostream>
#include<string>
using namespace std;
class building
{
    
    
	//全局函数是Building好朋友,可以访问Building中私有成员

	friend void greatgay(building* buil);
public:
	building()
	{
    
    
		m_sittingroom = 233;
		m_bedroom = 137;
	}
public:
	string m_sittingroom;
private:
	string m_bedroom;
};
//全局函数
void greatgay(building *buil)
{
    
    
	cout << "全局函数在访问:" << buil->m_sittingroom << endl;
	cout << "全局函数在访问:" << buil->m_bedroom << endl;

}
void test01()
{
    
    
	building buil;
	greatgay(&buil);
}
int main()
{
    
    
	void test01();
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_54673833/article/details/114014809