Use of unary plus operator

前端 未结 4 1280
抹茶落季
抹茶落季 2021-01-04 19:09

I\'ve heard it is used as overloaded operator+ for example

class MyClass
{
    int x;
public:
    MyClass(int num):x(num){}
    MyClass operator+(const MyCla         


        
4条回答
  •  日久生厌
    2021-01-04 19:33

    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;
    }
    

提交回复
热议问题