在 C++ 中对两个参数使用 const 覆盖运算符;

发布于 2024-07-10 22:35:19 字数 528 浏览 9 评论 0原文

我正在尝试使用两个 const 参数创建一个重写的运算符函数,但我不知道如何做到这一点。 这是一个简单的示例:

class Number
{
    Number()
    {
        value = 1;
    };

    inline Number operator + (const Number& n)
    {
        Number result;

        result.value = value + n.value;
        return result;
    }

    int value;
}

我在这里尝试做的是将两个参数传递到加法函数中,这两个参数都是 const 并返回结果而不更改类中的任何内容:

const Number a = Number();
const Number b = Number();
Number c = a + b;

这可能吗?我将如何做到这一点?

谢谢,

I'm trying to create an overridden operator function using both const parameters, but I can't figure out how to do it. Here is a simple example:

class Number
{
    Number()
    {
        value = 1;
    };

    inline Number operator + (const Number& n)
    {
        Number result;

        result.value = value + n.value;
        return result;
    }

    int value;
}

What I am trying to do here is pass in two arguments into the addition function that are both const and return the result without changing anything in the class:

const Number a = Number();
const Number b = Number();
Number c = a + b;

Is this possible and how would I go about doing this?

Thanks,

Dan

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

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

发布评论

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

评论(4

染柒℉ 2024-07-17 22:35:19

inline 在类声明中被理解,因此您不需要指定它。

最惯用的是,您可以将 operator+ 设为在类定义之外声明的非成员函数,如下所示:

Number operator+( const Number& left, const Number& right );

如果需要访问,您可能需要将其设为该类的 friend Number 的内部结构。

如果您必须将其作为成员函数,那么您需要将函数本身设置为 const:

Number operator+( const Number& n ) const
{ // ...

对于像 Number 这样的类,operator+ 通常是根据 operator+ 实现的= 因为通常您希望所有常用运算符按预期工作,而 operator+= 通常更容易实现,并且 operator+ 往往不会在实现过程中损失任何效率分别地。

课内:

Number& operator+=( const Number& n );

课外:

Number operator+( const Number& left, const Number& right )
{
    return Number( left ) += right;
}

甚至:

Number operator+( Number left, const Number& right )
{
    return left += right;
}

inline is understood in class declarations so you don't need to specify it.

Most idiomatically, you would make operator+ a non-member function declared outside the class definition, like this:

Number operator+( const Number& left, const Number& right );

You might need to make it a friend of the class if it needs access to Number's internals.

If you have to have it as a member function then you need to make the function itself const:

Number operator+( const Number& n ) const
{ // ...

For classes like Number, operator+ is typically implemented in terms of operator+= as usually you want all the usual operators to work as expected and operator+= is typically easier to implement and operator+ tends not to lose any efficiency over implementing it separately.

Inside the class:

Number& operator+=( const Number& n );

Outside the class:

Number operator+( const Number& left, const Number& right )
{
    return Number( left ) += right;
}

or even:

Number operator+( Number left, const Number& right )
{
    return left += right;
}
荒芜了季节 2024-07-17 22:35:19
class Number
{
    Number()
    {
        value = 1;
    };

    inline Number operator + (const Number& n) const
    {
        Number result;

        result = value + n.value;
        return result;
    }

    int value;
}
class Number
{
    Number()
    {
        value = 1;
    };

    inline Number operator + (const Number& n) const
    {
        Number result;

        result = value + n.value;
        return result;
    }

    int value;
}
意中人 2024-07-17 22:35:19

怎么样:

inline Number operator + (const Number& n) const

How about:

inline Number operator + (const Number& n) const
鹿港巷口少年归 2024-07-17 22:35:19

虽然我觉得前面的答案已经足够好了,但我认为需要一些澄清。

运算符(通常)有两种类型:

第一种是非成员函数,第二种是成员函数,其参数是操作的“右操作数”,并且通常返回当前修改的对象。

例如,假设类 T 有一个运算符 §。 它可以写成非成员函数

T operator § (const T & lhs, const T & rhs)
{
   T result ;
   // do the lhs § rhs operation, and puts the result into "result"
   return result ;
}

或写成成员函数

T & T::operator § (const T & rhs)
{
   // do the "this § rhs" operation, and puts the result into "this"
   return *this ;
}

甚至(非常不寻常)写成另一个成员函数

T T::operator § (const T & rhs) const
{
   T result ;
   // do the "this § rhs" operation, and puts the result into "result"
   return result ;
}

通常,您应该更喜欢非成员函数,因为您不应该将其声明为友元。 因此,使用非成员非友元函数可以增强对象的封装性。

免责声明:还有其他风格,但我仅限于算术运算符,例如 +*/- 等,以及“可信的”运算符原型。

分析运算符的使用

+ 的情况下:

  1. 每个操作数都需要是常量,因为 < code>a = b + c 不得更改 b,也不得更改 c
  2. 您可以累积 +,例如 a = b + c + d + e,因此临时变量必须存在。
T operator § (const T & lhs, const T & rhs)
{
   T result ;
   // do the lhs § rhs operation, and puts the result into "result"
   return result ;
}

对于+=

  1. 您知道左操作数A(来自A += B)将被修改。
  2. 您知道左操作数 A(来自 A += B)是它自己的结果。

所以你应该使用:

T & T::operator += (const T & rhs)
{
   // do the "this § rhs" operation, and puts the result into "this"
   return *this ;
}

一如既往,过早的优化是万恶之源

我在生产代码中见过这种代码,所以,它确实发生了:

T & operator + (const T & lhs, const T & rhs)
{
   static T result ; // result is STATIC !!!!
   // do the lhs + rhs operation, and puts the result into "result"
   return result ;
}

作者希望节省一个临时的时间。 使用这种代码,编写 a = b + c + d 会产生有趣(且错误)的结果。

^_^

最后但并非最不重要的一点是,

我在 此页面。 该页面仍在建设中,但其主要用途(易于复制/粘贴工作原型)可能非常有用......

While I feel the previous answers are good enough, I believe some clarification is needed.

Operators come (usually) in two flavors

The first being the non-member functions, the second being the member function whose parameter is the "right operand" of the operation and which usually returns the current modified object.

For example, imagine there's an operator § for a class T. It could be written either as a non-member function:

T operator § (const T & lhs, const T & rhs)
{
   T result ;
   // do the lhs § rhs operation, and puts the result into "result"
   return result ;
}

or as a member function:

T & T::operator § (const T & rhs)
{
   // do the "this § rhs" operation, and puts the result into "this"
   return *this ;
}

or even (very unusually) as another member function:

T T::operator § (const T & rhs) const
{
   T result ;
   // do the "this § rhs" operation, and puts the result into "result"
   return result ;
}

Usually, you should prefer the non-member function, if only because you should not declare it friend. Thus, using non-member non-friend function enhance the encapsulation of your object.

Disclaimer: There are other flavors, but I'm limiting myself to arithmetic operators like +, *, /, -, etc. here, as well as "credible" operator prototypes.

Analyse your use of the operator

In the case of +:

  1. Each operand needs to be constant because a = b + c must not change b, nor c.
  2. You can accumulate +, like in a = b + c + d + e, so temporaries must exist.
T operator § (const T & lhs, const T & rhs)
{
   T result ;
   // do the lhs § rhs operation, and puts the result into "result"
   return result ;
}

In the case of +=:

  1. You know the left operand A (from A += B) will be modified.
  2. You know the left operand A (from A += B) is its own result.

So you should use:

T & T::operator += (const T & rhs)
{
   // do the "this § rhs" operation, and puts the result into "this"
   return *this ;
}

As always, premature optimization is the root of all evil

I've seen this kind of code in production code so, it does happen:

T & operator + (const T & lhs, const T & rhs)
{
   static T result ; // result is STATIC !!!!
   // do the lhs + rhs operation, and puts the result into "result"
   return result ;
}

The author hoped to economize one temporary. With this kind of code, writing a = b + c + d leads to interesting (and wrong) results.

^_^

Last but not least

I did write a list of operator overloading prototypes on this page. The page is still under construction, but its main use (easy to copy/paste working prototypes) can be quite useful...

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