[C++ Elementary] analysis of ostream&, operator<<, operator<<(ostream& _cout, const Date& d), bool-guided structure embedded comparison function

 Sort out the knowledge of C++ overloading and judgment 

Table of contents

1. bool operator>(const Date& d) const [bool-guided structure embedded comparison function]

1. The overall meaning of the code

2. The meaning of the two const in the code

二、friend ostream& operator<<(ostream& _cout, const Date& d)

1. Related basic concepts 

2. Three references &


The problem comes from the following date class

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
	bool operator<(const Date& d)const
	{
		return (_year < d._year) ||
			(_year == d._year && _month < d._month) ||
			(_year == d._year && _month == d._month && _day < d._day);
	}
	bool operator>(const Date& d)const
	{
		return (_year > d._year) ||
			(_year == d._year && _month > d._month) ||
			(_year == d._year && _month == d._month && _day > d._day);
	}
	friend ostream& operator<<(ostream& _cout, const Date& d)
	{
		_cout << d._year << "-" << d._month << "-" << d._day;
		return _cout;
	}
private:
	int _year;
	int _month;
	int _day;
};



int main()
{
    Date date;
    cout<<date<<endl;
}

1. bool operator>(const Date& d) const [bool-guided structure embedded comparison function]

1. The overall meaning of the code

We can intercept a piece of code for analysis

bool operator>(const Date& d) const

The > overload of our common date class has the following form. It can be seen from observation that the > and < overload codes mentioned at the beginning are only derivatives of the following codes, and the essence has not changed

bool operator> (const Date& d) const
{
	if (_year > d._year ||
		_year == d._year && _month > d._month ||
		_year == d._year && _month == d._month && _day > d._day) 
		return true;
	else
		return false;
}

2. The meaning of the two const in the code

  • The first const is to prevent the referenced Date parameter from being modified;
  • The second const is a const member function. As for why it is needed, please refer to my other article.

const member function https://blog.csdn.net/Captain_ldx/article/details/127334021


二、friend ostream& operator<<(ostream& _cout, const Date& d)

Next we explain this code 

friend ostream& operator<<(ostream& _cout, const Date& d)

1. Related basic concepts 

First of all, we need to know the relevant basic concepts

  • ostream is the abbreviation of outputstream, that is, output stream. Used for output in C++, a typical output stream object is our common cout. In C++, you rarely customize the ostream object, and more often use cout directly.
  • The type of ostream usually appears in operator overloading such as <<, and appears as a friend function of a certain class. Taking the above code as an example, if you call an object of class Date (assuming I named it val), cout<<val is equivalent to calling this function, which usually outputs some member variables of Date.
  • friend Modified friend function
  • operator overloaded operator, the above code overloaded <<

2. Three references &

The above 3 references of ostream& operator<<(ostream& _cout, const Date& date) &

1. The first ostream& returns a reference to an ostream class object. In order to perform continuous operations , such as cout<<a<<b; first perform cout<<a, and after the operation is complete, return the reference to cout, that is, return cout In itself, the original formula becomes cout<<b; so that it can be written continuously, cout<<a<<b<<c<<.....

2. The second ostream& _cout, _cout is a random name, it is the object of the ostream class, and the usage is the same as cout, its purpose is: you can pass the data to _cout through <<, and finally use _cout as a parameter outgoing. So use cout<<date (date is an object of the date class) when outputting, and then call operator<<(_cout,d) to get the value of d (here d is the date data in the example), and finally Pass cout as a parameter.

For the code _cout<<date<<endl; in the beginning example , it is actually executed:

		_cout << d._year << "-" << d._month << "-" << d._day;
		return _cout;

 The above code is a derivative of the code shown below, and the difference is only due to the different personal code styles. The specific meaning is the same.

    cout<<date.year<<'-'<<date.month<<'-' <<date.day; 
    return cout;

3. The third const Date& is mainly for quick entry and exit of parameters . If & is not used, a parameter will be copied instead of passing in the original object directly, and the copy constructor will be called (if not written, the compiler will automatically provide it) , wasting time and sometimes producing errors.

For example, when there is a pointer in the data member, the copy constructor provided by the compiler will automatically copy a pointer, pointing to the same address as the original pointer, so that changing the address of one will change the other, which is what we don’t want .

When we need it, we need to copy a new pointer to point to a different address. The two objects do not interfere with each other, as long as the values ​​of the variables pointed to by the pointer are equal, we need to write our own copy constructor, new and so on. So generally use &, add const to prevent accidental modification.


The above is all the content of this article. If it is helpful to you, you may wish to like, bookmark, and follow. Thank you for reading.

Guess you like

Origin blog.csdn.net/Captain_ldx/article/details/129652275