1.
#include<iostream> using namespace std; class Sum { int add; public: Sum(int add) { this->add = add; } int operator+(const Sum & sum) const { int res; res = this->add + sum.add; return res; } int show() { return add; } }; int main() { Sum sum1(10); Sum sum2(20); int c=sum1.operator+(sum2); cout << c << endl; int d = sum1 + sum2; //等同于 cout << d << endl; }
2.运算符重载限制
重载运算符时操作数至少有一个是用户定义的类型。
3.不能创建新运算符
4.不能重载以下运算符:
sizeof
. .* :: ?:
typeid const_cast dynamic_cast
reinterpret_cast static_cast
5.可重载的运算符
+ - * / % ^ & | ~= ! = < > += -= *= /= %= ^= &= |=
<< >> >>= <<= == != <= >= && || ++ -- , ->* ->
() [] new delete new[] delete[]
原文:https://www.cnblogs.com/buchizaodian/p/11597960.html