关于重载运算符的问题+
考虑以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
否。因为 LHS 是一个
const A&
而 RHS 是一个int
,它会调用*,因为您只提供了非
const
版本const A& operator+( int m )
,编译器会抱怨。*:或者
operator+(const int& rhs) const
或者operator+(float rhs) const
...关键点是它必须是一个const
方法。No. Since the LHS is a
const A&
and RHS is anint
, it will call*as you've only provided the non-
const
versionconst A& operator+( int m )
, the compiler will complain.*: Or
operator+(const int& rhs) const
oroperator+(float rhs) const
... The crucial point is that it must be aconst
method.operator+
应该返回一个实例,而不是一个引用:要解释的具体问题是“返回一个常量引用,然后传递给
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: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 thenoperator+
is acting likeoperator +=
.因为您在添加时正在修改左侧对象。您不能使用
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.该函数需要是 const:
const A&运算符+( int m ) const;
The function needs to be const:
const A& operator+( int m ) const;
作为 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)const
或A&&运算符+(const A& rhs)const
;As
const A& operator+( const A& )
returns a const reference a non-const member functionconst 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 asconst 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
orA&& operator+(const A& rhs)const
;问题是
(a+a)
返回一个所谓的右值(基本上是临时的一个奇特术语)。虽然您可以对右值调用成员函数,但只能调用 const 成员函数。另外,每个人都说operator+
必须始终返回一个新值。您的运算符应该像这样实现:
但是,不修改其左参数的二元运算符可能更好地实现为自由函数:
通常,它们在
operator+=
之上实现,后者实现为会员: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 invokeconst
member functions. Also, everyone is right in saying thatoperator+
must alwasys return a new value.Your operators should be implemented like this:
However, binary operators which don't modify their left argument are probably better implemented as free functions:
Usually, they are implemented on top of
operator+=
, which is implemented as a member: