c++ 运算符重载

双目运算符:推荐重载为友元函数
成员函数:int operator+ (const T& a); 左值为隐藏参数 *this
友元函数:friend int oprator+ (const T& a, const E& b); 左右值不可互换位置。交换律需要再重载一个friend int oprator+ (const E& b, const T& a);

单目运算符:推荐重载为成员函数
成员函数:int operator++();
友元函数:friend int operator++(const T& a);
有前置和后置区别时,默认前置,后置额外加个int:
成员函数:int operator++(int);
友元函数:friend int operator++(const T& a, int);

你可能感兴趣的:(c++ 运算符重载)