C++ 中的运算符重载 作为 int + 对象

发布于 2024-07-29 08:50:36 字数 490 浏览 7 评论 0原文

我有以下课程:-

class myclass
{
    size_t st;

    myclass(size_t pst)
    {
        st=pst;
    }

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

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

};

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

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

但我无法做到这一点:-

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

任何想法,我怎样才能实现这一点?

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巾帼英雄 2024-08-05 08:50:36

在类外部实现运算符重载:

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

    int i;
};

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

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;
}
风吹短裙飘 2024-08-05 08:50:36

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

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

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

int operator+( int lhs, const myclass& rhs ) {
    return lhs + (int)rhs;
}
和影子一齐双人舞 2024-08-05 08:50:36

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

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

这种方法的主要好处之一是您的函数可以相互实现,从而减少总体代码量你需要。

更新:

为了避免性能问题,我可能会将非成员运算符+定义为内联函数,如下所示:

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

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

最后,正如 jalf 所指出的,一般需要考虑允许隐式转换的后果。 上面的示例假设从整型类型转换为“Num”是明智的。

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:

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 int objects.

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'.

故人爱我别走 2024-08-05 08:50:36

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

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

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

int operator+( int intobj, myclass myobj )
{ return intobj + int(myobj); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文