在 C++ 中对两个参数使用 const 覆盖运算符;
我正在尝试使用两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
inline
在类声明中被理解,因此您不需要指定它。最惯用的是,您可以将
operator+
设为在类定义之外声明的非成员函数,如下所示:如果需要访问,您可能需要将其设为该类的
friend
Number
的内部结构。如果您必须将其作为成员函数,那么您需要将函数本身设置为 const:
对于像
Number
这样的类,operator+
通常是根据operator+ 实现的=
因为通常您希望所有常用运算符按预期工作,而operator+=
通常更容易实现,并且operator+
往往不会在实现过程中损失任何效率分别地。课内:
课外:
甚至:
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:You might need to make it a
friend
of the class if it needs access toNumber
's internals.If you have to have it as a member function then you need to make the function itself const:
For classes like
Number
,operator+
is typically implemented in terms ofoperator+=
as usually you want all the usual operators to work as expected andoperator+=
is typically easier to implement andoperator+
tends not to lose any efficiency over implementing it separately.Inside the class:
Outside the class:
or even:
怎么样:
How about:
虽然我觉得前面的答案已经足够好了,但我认为需要一些澄清。
运算符(通常)有两种类型:
第一种是非成员函数,第二种是成员函数,其参数是操作的“右操作数”,并且通常返回当前修改的对象。
例如,假设类
T
有一个运算符§
。 它可以写成非成员函数:或写成成员函数:
甚至(非常不寻常)写成另一个成员函数:
通常,您应该更喜欢非成员函数,因为您不应该将其声明为友元。 因此,使用非成员非友元函数可以增强对象的封装性。
免责声明:还有其他风格,但我仅限于算术运算符,例如
+
、*
、/
、-
等,以及“可信的”运算符原型。分析运算符的使用
在
+
的情况下:b
,也不得更改c
。+
,例如a = b + c + d + e
,因此临时变量必须存在。对于
+=
:所以你应该使用:
一如既往,过早的优化是万恶之源
我在生产代码中见过这种代码,所以,它确实发生了:
作者希望节省一个临时的时间。 使用这种代码,编写
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 classT
. It could be written either as a non-member function:or as a member function:
or even (very unusually) as another member function:
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
+
:a = b + c
must not changeb
, norc
.+
, like ina = b + c + d + e
, so temporaries must exist.In the case of
+=
:So you should use:
As always, premature optimization is the root of all evil
I've seen this kind of code in production code so, it does happen:
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...