【c++】Friend

【c++】Friend

     I.  Tomomoto

     2.  Friend function

     3. Tomomoto Metaclass

refer to:

"C++ from entry to proficient" People's Posts and Telecommunications Publishing House

  

Tomomoto

        In C++, in order to make the private members and protected members of class A accessible by other classes or other member functions, the concept of friend is introduced. For example, if you find a good friend class B for class A, then class B can access the private members and protected members of class A.

        A friend can be an ordinary function, a member function of another class, or another class!

 

friend function

        When an ordinary function wants to access the private and protected member data of one class, it must be implemented by calling the public member function because it is not allowed to directly access the private and protected members of another class, so the access efficiency is very low. In order to improve access efficiency , C++ allows a common function (or a member function of another class or another class) to be declared as a friend function (or friend class) in a class.

        Friend keyword: friend .

 

Friend function example:

//Friend function.cpp

#include<iostream>
using namespace std;

class Chat
{
	private:
		int x;
		int y;
	public:
		
		void set(int a,int b)
		{
			x=a;
			y=b;
		}
		void print()
		{
			cout<<"x="<<x <<" y="<<y <<endl;
		}
		
		// friend function
		friend Chat add (Chat & a, int nOffset);
};

Chat add (Chat & a, int nOffset)
{
	Chat aTemp;
	aTemp.x=a.x+nOffset;
	aTemp.y=a.y+nOffset;
	
	return aTemp;

}


intmain()
{
	Chat m;
	m.set(4,5);
	cout<<"Before calling friend function: ";
	m.print();
	
	m=add(m,5); //call friend function
	cout<<"After calling friend function: ";
	m.print();
	
	return 0;
}

operation result:

                     

analyze:

        In this example, the class Cset is defined, the friend function add() is declared in the class, and the function is defined outside the class.

        The object Cset m is created in main(), and after the object is created, m.set(4,5) is called to set the value of x, y to 4, 5; then the friend function add() is called to realize the private variable value x, y Modifications.

        In general, friend functions should pay attention to:

(1) Friend functions must be specified in the class definition . It can be used in private, protected, and public, and the location is not limited.

(2) The friend function is not a member of the class, so it cannot directly refer to the name of the object member, nor can it refer to the member of the object through the this pointer, but must refer to the member of the object through the object name or object pointer passed in by the entry parameter . Therefore, friend functions generally have an entry parameter of the class.

Add : Chat add (Chat & a, int nOffset)

(3) When a function needs to access multiple classes , the function should be defined as a friend function of these classes at the same time.

 

Tomomoto Metaclass

        Not only a function can be a friend of a class, but a class can also be a friend of another class, such a class is called a friend class.

 

Friend class programming example:

//Friend class.cpp

#include<iostream>
using namespace std;

class A
{
	private:
		int x;
		static int y;
	public:
		
		void set(int a)
		{
			x=a;
		}
		void print()
		{
			cout<<"x="<<x <<" y="<<y <<endl;
		}
		
		// Tomomoto
		friend class B;
};

class B
{
	private:
		A a;
	public:		
		B(int m,int n)
		{
			a.x=m;
			ay=n;
		}
		void print()
		{
			cout<<"x="<<a.x <<" y="<<a.y <<endl;
		}
};

int A::y=1; //Note

intmain()
{
	A a;
	a.set(5);
	a.print();
	
	B b(6,7);
	b.print();
	a.print();
	
	return 0;
}

operation result:

        

 

Note : It is error to initialize static variables directly in the class.

class A
{
	private:
		int x;
		static int y=1; // error!
}

        Among them, static inty=1; will report an error : c++ prohibits the initialization of non-const static member variables in a class.

        Static variables are initialized only once, whether in a class or in a function . Non-const static variables defined in a class should be initialized outside the class. Change the above to:

class A
{
	private:
		int x;
		static int y;  //
}
int A::y=1; //Note initialization outside the class

        If it is a static variable of const type, it can be initialized directly in the class.

class A
{
	private:
		int x;
		static const int y=1;  // OK
}
  -------------------------------------------          END       -------------------------------------

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817917&siteId=291194637