本文介绍了使用一元加运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我听说它用作重载运算符+,例如
I've heard it is used as overloaded operator+ for example
class MyClass
{
int x;
public:
MyClass(int num):x(num){}
MyClass operator+(const MyClass &rhs)
{
return rhs.x + x;
}
};
int main()
{
MyClass x(100);
MyClass y(100);
MyClass z = x + y;
}
这真的是一元加运算符的使用,操作符?
Is this really the use of unary plus operator or is it really a binary + operator?
推荐答案
这不是重载和使用一元+ ..你需要做一个自由功能或使成员function take 0 arguments
This is not overloading and using unary + .. You need to either make that a free function or make the member function take 0 arguments
class MyClass
{
int x;
public:
MyClass(int num):x(num){}
MyClass operator+() const
{
return *this;
}
};
int main() {
MyClass x = 42;
+ x;
}
这篇关于使用一元加运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!