C++ 中的运算符重载为 int + obj

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1188566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:07:05  来源:igfitidea点击:

Operator Overloading in C++ as int + obj

c++operatorsoperator-overloadingoperator-keyword

提问by Azher Iqbal

I have following class:-

我有以下课程:-

class myclass
{
    size_t st;

    myclass(size_t pst)
    {
        st=pst;
    }

    operator int()
    {
        return (int)st;
    }

    int operator+(int intojb)
    {
        return int(st) + intobj; 
    }

};

this works fine as long as I use it like this:-

只要我这样使用它就可以正常工作:-

char* src="This is test string";
int i= myclass(strlen(src)) + 100;

but I am unable to do this:-

但我无法做到这一点:-

int i= 100+ myclass(strlen(src));

Any idea, how can I achieve this??

任何想法,我怎样才能做到这一点?

回答by Brian R. Bondy

Implement the operator overloading outside of the class:

在类外实现运算符重载:

class Num
{
public:
    Num(int i)
    {
        this->i = i;
    }

    int i;
};

int operator+(int i, const Num& n)
{
    return i + n.i;
}

回答by Jeff L

You have to implement the operator as a non-member functionto allow a primitive int on the left hand side.

您必须将运算符实现为非成员函数,以允许在左侧使用原始 int。

int operator+( int lhs, const myclass& rhs ) {
    return lhs + (int)rhs;
}

回答by Richard Corden

The other answers here will solve the problem, but the following is the pattern I use when I'm doing this:

此处的其他答案将解决问题,但以下是我在执行此操作时使用的模式:

class Num
{
public:
  Num(int i)       // Not explicit, allows implicit conversion to Num
  : i_ (i)
  {
  }

  Num (Num const & rhs)
  : i_ (rhs.i_)
  {
  }

  Num & operator+= (Num const & rhs)  // Implement +=
  {
    i_ += rhs.i_;
    return *this;
  }

private:
    int i_;
};

//
// Because of Num(int), any number on the LHS or RHS will implicitly
// convert to Num - so no need to have lots of overloads
Num operator+(Num const & lhs, Num const & rhs)
{
  //
  // Implement '+' using '+='
  Num tmp (lhs);
  tmp+=rhs;
  return tmp;
}

One of the key benefits of this approach is that your functions can be implemented in terms of each other reducing the amount of overall code you need.

这种方法的主要好处之一是您的功能可以相互实现,从而减少了您需要的整体代码量。

UPDATE:

更新:

To keep performance concerns at bay, I would probably define the non member operator+ as an inline function something like:

为了避免性能问题,我可能会将非成员 operator+ 定义为内联函数,例如:

inline Num operator+(Num lhs, Num const & rhs)
{
  lhs+=rhs;
  return lhs;
}

The member operations are also inline (as they're declared in the class body) and so in all the code should be very close to the cost of adding two raw intobjects.

成员操作也是内联的(因为它们是在类主体中声明的),因此在所有代码中应该非常接近添加两个原始int对象的成本。

Finally, as pointed out by jalf, the consequences of allowing implicit conversions in general needs to be considered. The above example assumes that it's sensible to convert from an integral type to a 'Num'.

最后,正如 jalf 所指出的,通常需要考虑允许隐式转换的后果。上面的示例假定从整数类型转换为 'Num' 是明智的。

回答by Peter Kovacs

You need a global function operator+( int, myclass ) to do this:

您需要一个全局函数 operator+( int, myclass ) 来执行此操作:

int operator+( int intobj, myclass myobj )
{ return intobj + int(myobj); }