关于重载运算符的问题+

发布于 2024-08-23 22:48:25 字数 614 浏览 4 评论 0原文

考虑以下代码:


class A
{
public:
    A& operator=( const A& );
    const A& operator+( const A& );
    const A& operator+( int m );
};

int main()
{
    A a;
    a = ( a + a ) + 5;   // error: binary '+' : no operator found which takes a left-hand operand of type 'const A'
}

任何人都可以解释为什么上面的内容会作为错误返回吗?

( a + a )”调用“const A&operator+(const A&)”并返回一个常量引用,然后将其传递给“const A&operator+(const A&)”。如果我没记错的话,operator+( int m )”。

如何修复上述错误(无需创建全局二元运算符+或接受 int 的构造函数),以便允许 main() 内的语句?

Consider the following code:


class A
{
public:
    A& operator=( const A& );
    const A& operator+( const A& );
    const A& operator+( int m );
};

int main()
{
    A a;
    a = ( a + a ) + 5;   // error: binary '+' : no operator found which takes a left-hand operand of type 'const A'
}

Can anyone explain why the above is returned as an error?

"( a + a )" calls "const A& operator+( const A& )" and returns a constant reference which is then passed to "const A& operator+( int m )" if I'm not mistaken.

How can one fix the above error (without creating a global binary operator+ or a constructor that accepts an int) such that the statement inside main() is allowed?

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

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

发布评论

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

评论(6

绝對不後悔。 2024-08-30 22:48:25

如果我没记错的话,它会被传递给“const A& operator+( int m )”

否。因为 LHS 是一个 const A& 而 RHS 是一个 int ,它会调用*,

[anyType] operator+ (int rhs) const
//                            ^^^^^ note the const here.

因为您只提供了非 const 版本 const A& operator+( int m ),编译器会抱怨。

*:或者operator+(const int& rhs) const或者operator+(float rhs) const...关键点是它必须是一个const 方法。

which is then passed to "const A& operator+( int m )" if I'm not mistaken

No. Since the LHS is a const A& and RHS is an int, it will call*

[anyType] operator+ (int rhs) const
//                            ^^^^^ note the const here.

as you've only provided the non-const version const A& operator+( int m ), the compiler will complain.

*: Or operator+(const int& rhs) const or operator+(float rhs) const... The crucial point is that it must be a const method.

当梦初醒 2024-08-30 22:48:25

operator+ 应该返回一个实例,而不是一个引用:

// as member function
A operator+(const A& other);

// as free function
A operator+(const A& left, const A& right);

要解释的具体问题是“返回一个常量引用,然后传递给 const A&operator+( int m )”。由于您有一个 const 引用,因此它无法调用该函数,因为它不是 const 方法(即 const A& operator+( int m ) const )。

也就是说,这不是修复operator+的方法。如果您要返回一个引用,那么它是对什么的引用?运算符 + 中的本地会很糟糕,因为您不应该返回对本地的引用。对全局的引用会很糟糕,因为它会限制代码的正确使用方式。对已分配内存的引用是不好的,因为它会泄漏内存。对 *this 的引用会很糟糕,因为 operator+ 的作用类似于 operator +=

operator+ should return an instance, not a reference:

// as member function
A operator+(const A& other);

// as free function
A operator+(const A& left, const A& right);

To explain the specific problem is "returns a constant reference which is then passed to const A& operator+( int m )". Since you have a const reference, it cannot call that function because it's not a const method (i.e. const A& operator+( int m ) const).

That said, that is not the way to fix operator+. If you're returning a reference, what is it a reference to? A local in operator+ would be bad as you shouldn't return a reference to a local. A reference to a global would be bad because it will limit how your code can be used properly. A reference to allocated memory would be bad because it will leak memory. A reference to *this would be bad because then operator+ is acting like operator +=.

十年九夏 2024-08-30 22:48:25

因为您在添加时正在修改左侧对象。您不能使用 const 对象来做到这一点。顺便说一句,接受塞缪尔的建议,因为惯用的方法是返回所添加对象的新副本。

Because you are modifying the left-hand side object when adding. You can't do that with a const object. Take Samuel advice btw, because the idiomatic way is to return a new copy of the added objects.

疏忽 2024-08-30 22:48:25

该函数需要是 const:

const A&运算符+( int m ) const;

The function needs to be const:

const A& operator+( int m ) const;

肥爪爪 2024-08-30 22:48:25

作为 const A& operator+( const A& ) 返回一个非 const 成员函数 const 引用 const A&不能通过 const 对象调用operator+( int m )。
或者,第一个运算符应定义为 A& operator+( const A& ) 或者,第二个运算符为 const A&运算符+(int m)const;
然而,这些更改只会使它们在技术上正确,而不是在大多数情况下在美观上正确,因为二元运算符不应修改任何输入参数,但仍要计算结果并返回。因此,结果必须按值返回,或者在 C++0x 的情况下,作为右值引用返回。
A运算符+(const A& rhs)constA&&运算符+(const A& rhs)const;

As const A& operator+( const A& ) returns a const reference a non-const member function const A& operator+( int m ) can not be called over a const object.
Either, the first operator should be defined as A& operator+( const A& ) or, the second operator as const A& operator+( int m )const;
However, these changes will only make them technically correct, not aesthetically in majority of the cases, as a binary operator is not supposed to modify any of the input argument and yet to compute a result and return. Thus the result have to be returned by value or in case of C++0x , as a r-value reference.
i.e A operator+(const A& rhs)const or A&& operator+(const A& rhs)const;

菊凝晚露 2024-08-30 22:48:25

问题是 (a+a) 返回一个所谓的右值(基本上是临时的一个奇特术语)。虽然您可以对右值调用成员函数,但只能调用 const 成员函数。另外,每个人都说 operator+ 必须始终返回一个新值。

您的运算符应该像这样实现:

A operator+( const A& ) const;
A operator+( int m ) const;

但是,不修改其左参数的二元运算符可能更好地实现为自由函数:

class A { ... };

A operator+(const A& lhs, const A& rhs);
A operator+(const A& lhs, int rhs);

通常,它们在 operator+= 之上实现,后者实现为会员:

class A {
  public:
   A& operator+=(const A& rhs);
   A& operator+=(int rhs);
};

inline A operator+(A lhs, const A& rhs) // note: lhs is passed by copy now
{
  lhs += rhs;
  return lhs;
}
A operator+(A lhs, int rhs) // note: lhs is passed by copy now
{
  lhs += rhs;
  return lhs;
}

The problem is that (a+a) returns a so-called rvalue (basically a fancy term for a temporary). While you can invoke member functions on an rvalue, you can only invoke const member functions. Also, everyone is right in saying that operator+ must alwasys return a new value.

Your operators should be implemented like this:

A operator+( const A& ) const;
A operator+( int m ) const;

However, binary operators which don't modify their left argument are probably better implemented as free functions:

class A { ... };

A operator+(const A& lhs, const A& rhs);
A operator+(const A& lhs, int rhs);

Usually, they are implemented on top of operator+=, which is implemented as a member:

class A {
  public:
   A& operator+=(const A& rhs);
   A& operator+=(int rhs);
};

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