如何在 C++ 中定义一侧运算符?
How to define one side operators in c++?
如何在 C++ 中定义像 ++
或 --
这样的一侧运算符?
例如我们要定义a##
做(a % 45) + 2
[这只是一个例子]
有operator ++()
(前缀递增运算符)
和 operator ++(int)
(后缀递增运算符)
与 operator --
相同。
class Example
{
public:
int a = 0;
Example& operator++() { a = (a % 45) + 2; return *this; } // ++ex;
Exampleoperator++(int) { Example tmp = *this; ++(*this); return tmp; } // ex++;
};
没有operator ##
.
如何在 C++ 中定义像 ++
或 --
这样的一侧运算符?
例如我们要定义a##
做(a % 45) + 2
[这只是一个例子]
有operator ++()
(前缀递增运算符)
和 operator ++(int)
(后缀递增运算符)
与 operator --
相同。
class Example
{
public:
int a = 0;
Example& operator++() { a = (a % 45) + 2; return *this; } // ++ex;
Exampleoperator++(int) { Example tmp = *this; ++(*this); return tmp; } // ex++;
};
没有operator ##
.