C++ Primer 5th notes (chap 14 overloaded operations and type conversion) arithmetic and relational operators

Operator
Binocular arithmetic operators + (Addition), -(subtraction), * (multiplication), / (division),% (modulo)
Relational operator == (equal to), != (not equal to), <(less than),> (greater than>, <= (less than or equal to), >= (greater than or equal to)
  • Under normal circumstances, arithmetic and relational operators should be defined as non-member functions so that the operands on both sides can be converted.
  • Since these operators generally do not change the state of the operand, the formal parameters are constant references.
  • If the class defines both arithmetic operators and related compound assignment operators, you should use compound assignment to implement the arithmetic operators.

1. Arithmetic Operators

It usually calculates its two operands and obtains a new value. This value is usually stored in a local variable. After the operation is completed, a copy of the local variable is returned as the result (the return type is recommended to be set to the const type of the original object).

Sales_data operator +(const Sales_data &lhs,const Sales_data &rhs)
{
    
    
	Sales_data sum = lhs;
	sum += rhs;
	return sum;
}

2. Equality Operators

Design criteria:

  • If the class has the meaning of equality in logic, operator== should be defined instead of an ordinary named function. This makes it easier to use standard library containers and algorithms, and easier to remember.
  • Under normal circumstances, operator== should be transitive.
  • If the class defines operator==, then operator!= should also be defined.
  • One of operator== and operator!= should delegate specific work to the other.
bool operator==(const Sales_data &lhs, const Sales_data &rhs)
{
    
    
    return lhs.isbn() == rhs.isbn() &&
        lhs.units_sold == rhs.units_sold &&
        lhs.revenue == rhs.revenue;
}

bool operator!=(const Sales_data &lhs, const Sales_data &rhs)
{
    
    
    return !(lhs == rhs);
}

3. Assignment Operator

  • Regardless of the type of the formal parameter, the assignment operator operator must be defined as a member function, and the compound assignment operator is usually the same.
  • Both types of operators should return a reference to the operand on the left (assignment operator and compound assignment operator).
  • You can use other types as the right side of the operation object
//重载赋值运算符
StrVec &StrVec::operator=(initializer_list<string> il)
{
    
    
    //alloc_copy分配内存空间,并从给定安慰内拷贝元素
    
    auto data=alloc_n_copy(il.begin(),il.end());
    free();  // 销毁对象中的元素并释放内存空间

    elements=data.frist;//更新数据成员,使其指向新空间

    first_free=cap=data.second; 
    return *this;  

} 
 
//复合赋值运算符 
// member binary operator: left-hand operand is bound to the implicit this pointer
// assumes that both objects refer to the same book
Sales_data& Sales_data::operator+=(const Sales_data &rhs)
{
    
    
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}

4. Relational Operators

Define the order relationship. eg. Less than operator used in associative containers

  • If a class is logically equivalent, then the class should define operator==.
  • Classes that define equality operators usually also define relational operators.
  • If the class defines operator==, the definition of the relational operator should be consistent with operator==. In particular, if two objects are not equal, then one of the objects should be smaller than the other.
  • If there is only one reliable <definition, you should consider defining the <operator for this class. If the class also contains ==, then the <operator is defined if and only if the definition of <is consistent with the result produced by ==.
//重载复合赋值运算符
StrVec &StrVec::operator+=(initializer_list<string> il)
{
    
    
   unit_sold+=rhs.units_sold;
   revenue+=rhs.revenue;
   return *this;
}

Guess you like

Origin blog.csdn.net/thefist11cc/article/details/113927930