[C++] classes and objects (2 supplementary operator overloading, const members)

[C++] class and object (2 supplementary operator overloading, const member)

Author: Gangzi who loves to write code
Time: 2023.5.11
This article serves as a supplement to [C ++] classes and objects (2) (operator overloading, const members)

Table of Contents

  • [C++] Classes and objects (2 supplementary operator overloading, const members)
      • assignment operator overloading
          • operator overloading
            • Operator overloading for pre ++ and post ++ for the Date class (Date)
            • Implement operator overloading for << of the date class (Date):
          • assignment operator overloading
      • const member
          • Member functions of const modified classes
      • Address and const address operator overloading

Assignment operator overloading

Operator overloading

C++ introduces operator overloading to enhance the readability of the code. Operator overloading is a function with a special function name and also has its return value class
Type, function name and parameter list, its return value type and parameter list are similar to ordinary functions.
Function name: The keyword operator is followed by the operator symbol that needs to be overloaded.
Function prototype: return value type operator operator (parameter list)

Notice:

  • New operators cannot be created by concatenating other symbols: e.g. operator@
  • The overloaded operator must have an operand of class type or enumeration type.
  • Operators for built-in types whose meaning cannot be changed, for example: the built-in integer + cannot change its meaning
  • As an overloaded function of a class member, its formal parameters appear to be 1 less than the number of operands The operator of the member function has a default formal parameter this, limited to the first formal parameter
  • .* , :: , sizeof , ?: , . Note that the above 5 operators cannot be overloaded (very important).
  • C++ stipulates that the assignment operator “=” can only be overloaded as a non-static member function of a class, but not a friend function of a class.

The following is an example of the date class:

class Date
{<!-- -->
public:
Date::Date(int year, int month, int day)
{<!-- -->
_year = year;
_month = month;
_day = day;
}
Date::operator=(const Date & d)
{<!-- -->
_year = d._year;
_month = d._year;
_day = d._day;
}
private:

int_year;
int_month;
int_day;
};

Note: bool operator==(Date* this, const Date & amp; d2);
The left operand is the object of the calling function pointed to by this

  • Attached:
Operator overloading for the date class (Date) pre ++ and post ++
//prefix ++
Date & Date::operator ++ ()
{<!-- -->
*this += 1;
return *this;
}

// post ++
// Adding this int parameter is not to receive a specific value, but just a placeholder (bypassing grammatical restrictions), which constitutes an overload with the prefix ++
Date Date::operator + + (int)
{<!-- -->
Date tmp = *this;
*this += 1;
\t
return tmp;
}

The post ++ should use the int parameter to occupy the place, in order to distinguish it from the pre ++ (– is the same as + +)

Realize operator overloading of Date class (Date) to <<:

Note that stream insertion cannot be written as a member function, because the Date object takes the first parameter by default and is used as the left operand, which does not conform to usage habits
Since it cannot be written as a member function, how to access the private variables in the class?
Solution:

  1. Change the overloaded function of the << operator into a friend function of Date. (C++ commonly used)
  2. Call functions (public) such as GetYear in Date to access. (commonly used in Java)
//Pay attention to the use of const
ostream & amp; operator<<(ostream & amp; out, const Date & amp; d)
{<!-- -->
out << d._year << "year" << d._month << "month" << d._day << "day" << endl;

return out;
}

istream & amp; operator>>(istream & amp; in, Date & amp; d)
{<!-- -->
int year, month, day;
in >> year >> month >> day;

if (month > 0 & amp; & amp; month < 13
& amp; & amp; day > 0 & amp; & amp; day <= d.GetMonthDay(year, month))
{<!-- -->
d._year = year;
d._month = month;
d._day = day;
}
else
{<!-- -->
cout << "Illegal Date" << endl;
assert(false);
}

return in;
}
  • << can directly support the built-in type is implemented in the library
  • << can directly support self-defined identification types because of function overloading
  • The execution order of << is from left to right
  • Operator overloading can also be called as a function display, of course, this is definitely not good.
Assignment operator overloading

There are four main points of assignment operator:

  1. Parameter Type
  2. return value
  3. Check if you assign a value to yourself
  4. return *this
  5. If a class does not explicitly define an assignment operator overload, the compiler will also generate one, to complete the value copy of the object in byte order. (same as the copy function should pay attention to)

The difference between assignment operator overloading and copy constructor:
The copy constructor is to create a copied object
The assignment operator overload is copied to an existing object

const member

Member function of const modified class

The const-decorated class member function is called a const member function. The const-modified class member function actually modifies the implicit this pointer of the member function, indicating that any member of the class cannot be modified in the member function.
Example:
If there is a print() member function in the Date class

void print()
{<!-- -->
//...
}

Discover:

Date d1;
d1.print();//Normal (this pointer type: Date*)
const Date d2;
d2.print()//report an error (because print() may modify the const type, and the authority has been enlarged) (this pointer type: const Date*)

Solution:
Change the print member function to

void print() const//const modifies this pointer
{<!-- -->
//...
}
  • After adding const after the member function, both ordinary and const objects can call the member function (but the member function that needs to modify the member variable cannot add const), so as long as the function does not modify the member variable, const should be added.
  • If operator overloading is performed on <, the correct format is bool operator(const Date & amp; x) const; (const should be added to both sides of ) instead of bool operator (const Date & x);
  • The const modifier is not the this pointer, but *this (easy to mix), the this pointer itself is unmodifiable

Address and const address operator overloading

General custom types cannot use operators
However, these two default member functions generally do not need to be redefined, and the compiler will generate them by default.

Date* operator &()
{<!-- -->
return this;
}
 
const Date* operator &() const
{<!-- -->
return this;
}

Two functions constitute function overloading (parameters (this pointer) are of different types)
const objects (must be initialized) call const Date* operator & amp;() const, ordinary objects call Date* operator & amp;()
These two operators generally do not need to be overloaded, just use the default address-taking overload generated by the compiler. Only in special cases, overloading is required, such as not wanting others to get the specified content (returning nullptr when returning a pointer) .