int a, b, c;
//do stuff. For e.g., cin >> b >> c;
c = a + b; //works
c = operator+(a,b); //fails to compile, 'operator+' not defined.
另一方面,这有效 -
class Foo
{
int x;
public:
Foo(int x):x(x) {}
Foo friend operator+(const Foo& f, const Foo& g)
{
return Foo(f.x + g.x);
}
};
Foo l(5), m(10);
Foo n = operator+(l,m); //compiles ok!
- 甚至可以直接调用原始类型(如 int)的 operator+(和其他运算符)吗?
- 如果是,如何?
- 如果没有,是否有 C++ 参考措辞明确表明这是不可行的?