Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
说我有一个int,像这样:
int
int foo = 5;
然后我可以这样做:
int bar = -foo; // -5
我希望能够对我的班级做同样的事情,那么如何重载-运算符,用作* -1?我是否必须重载*操作员才能这样做?
-
* -1
*
class MyClass { friend MyClass operator-(const MyClass& x); };
or
class MyClass { MyClass operator-() const; };
Take your pick (although I would go for the first).