[C++ basic knowledge] friends, inner classes, friend function overloading <<and>>

Tomomoto

Friends are divided into friend functions and friend classes.
Friends provide a way to break through encapsulation and sometimes provide convenience. But friends will increase the coupling and destroy the package, so friends should not be used more .

Friend function

It can be a non-member function that does not belong to any class, or a member function of another class, collectively referred to as friend functions. Friend function is not a member function of the current class, but an external function independent of the current class, but it can access all members of the class, including private members, protected members and public members.
When declaring a friend function in a class, you need to add the friend keyword before the function name , which can be placed anywhere in the class without being restricted by access qualifiers. The definition can be placed inside or outside the class.

Code example:

Overload << and >>

class Date
{
    
    
	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend istream& operator>>(istream& _cin, Date& d);
public:
 	Date(int year, int month, int day)
 		: _year(year)
 		, _month(month)
 		, _day(day)
 {
    
    }
 
private:
 	int _year;
	int _month;
 	int _day;
};
	ostream& operator<<(ostream& _cout, const Date& d) 
	{
    
    
 		_cout<<d._year<<"-"<<d._month<<"-"<<d._day;
 		return _cout; 
	 }
	istream& operator>>(istream& _cin, Date& d) 
	{
    
    
		 _cin>>d._year;
		 _cin>>d._month;
		 _cin>>d._day;
		 return _cin; 
	}
int main()
{
    
    
 	Date d;
 	cin>>d;
 	cout<<d<<endl;
 	return 0; 
 }

This code uses friend functions to overload the "<<" and ">>" operators.

Description:
1. Friend functions can access private and protected members of the class, but not the member functions of the class.
2. Friend functions cannot be modified with const.
3. Friend functions can be declared anywhere in the class definition and are not restricted by class access. Character restriction
4. A function can be a friend function of multiple classes
5. The call of a friend function is the same as the call and principle of a normal function

Tomomoto metaclass

The description method of a friend class is to add a statement in the declaration of another class:

friend class name;

All member functions of a friend class can be friend functions of another class, and all can access non-public members in another class.
1. The friendship relationship is one-way and not interchangeable.

For example, Time class and Date class, declare the Date class as a friend class in the Time class, then >> can directly access the private member variables of the Time class in the Date class, but want to access the private members of the Date class in the Time class Variables do not work.

2. Friendship cannot be passed on

If B is a friend of A and C is a friend of B, then it cannot be said that C is a friend of A.

Inner class

Concept : If a class is defined inside another class, this class is called an inner class. Note that this inner class is an independent class at this time , it does not belong to the outer class, let alone call the inner class through the object of the outer class. The outer class does not have any superior access rights to the inner class.
Note : The inner class is the friend class of the outer class. Pay attention to the definition of the friend class, the inner class can access all the members in the outer class through the object parameter of the outer class. But the outer class is not a friend of the inner class.
Features :

  1. The inner class can be defined in the outer class. Public, protected, and private are all possible.
  2. Note that the inner class can directly access the static and enumeration members in the outer class, without the object/class name of the outer class.
  3. sizeof (external class) = external class, and internal class has no any

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/114800142