C++ 中的 const 正确性运算符重载返回
我有点困惑为什么我被告知要从 C++ 中的二元运算符返回 const foo 而不仅仅是 foo。
我一直在阅读 Bruce Eckel 的《Thinking in C++》,在关于运算符重载的章节中,他说“通过将[重载二元运算符]的返回值设置为 const,您可以声明只有 const 成员函数可以被调用以获取该返回值。这是常量正确的,因为它可以防止您将潜在有价值的信息存储在很可能丢失的对象中”。
但是,如果我有一个返回 const 的加号运算符和一个前缀增量运算符,则此代码无效:
class Integer{
int i;
public:
Integer(int ii): i(ii){ }
Integer(Integer&);
const Integer operator+();
Integer operator++();
};
int main(){
Integer a(0);
Integer b(1);
Integer c( ++(a + b));
}
为了允许这种赋值,让 + 运算符返回一个非常量值不是有意义吗?这可以通过添加 const_casts 来完成,但这会变得相当庞大,不是吗?
谢谢!
I'm a little confused as to why I've been told to return const foo from a binary operator in c++ instead of just foo.
I've been reading Bruce Eckel's "Thinking in C++", and in the chapter on operator overloading, he says that "by making the return value [of an over-loading binary operator] const, you state that only a const member function can be called for that return value. This is const-correct, because it prevents you from storing potentially valuable information in an object that will be most likely be lost".
However, if I have a plus operator that returns const, and a prefix increment operator, this code is invalid:
class Integer{
int i;
public:
Integer(int ii): i(ii){ }
Integer(Integer&);
const Integer operator+();
Integer operator++();
};
int main(){
Integer a(0);
Integer b(1);
Integer c( ++(a + b));
}
To allow this sort of assignment, wouldn't it make sense to have the + operator return a non-const value? This could be done by adding const_casts, but that gets pretty bulky, doesn't it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您说 ++x 时,您是在说“将 1 添加到 x,将结果存储回 x,然后告诉我它是什么”。这是预自增运算符。但是,在 ++(a+b) 中,你应该如何“将结果存储回 a+b”?
当然,您可以将结果存储回当前保存 a+b 结果的临时存储器中,该临时存储器很快就会消失。但是,如果您并不真正关心结果存储在哪里,为什么要增加它而不是只加一呢?
When you say ++x, you're saying "add 1 to x, store the result back into x, and tell me what it was". This is the preincrement operator. But, in ++(a+b), how are you supposed to "store the result back into a+b"?
Certainly you could store the result back into the temporary which is presently holding the result of a+b, which would vanish soon enough. But if you didn't really care where the result was stored, why did you increment it instead of just adding one?
仅供参考,即使对于 POD(普通旧数据类型,例如
int
),++(a + b)
也是非法的。因此,不允许您自己的类类型使用它也是有道理的。试试这个:GCC 返回
错误:需要左值作为增量操作数
。在您的情况下,最好让您的复制构造函数采用 const Integer 参数,并像这样创建您的 Integer c :
FYI,
++(a + b)
is illegal even with PODs (plain old data types, likeint
). So it makes sense not to allow it for your own class types either. Try this:GCC returns
error: lvalue required as increment operand
.In your case, it would be preferable make your copy constructor take a
const Integer
argument, and create yourInteger c
like this instead:复制构造函数通常采用 const 引用,为您解决该问题。
(拥有非常量复制构造函数意味着一些资源转移,这有时很有用,但对于 99% 的情况,不需要)
Copy constructors usually take a const reference, solving that problem for you.
(Having non-const copy ctor implies some transfer of resources, which can be useful sometimes, but for 99% of all situations, it's not needed)
我相信OP的例子将适合这个问题,如果加法运算符被任何其他返回引用的二元运算符替换,例如赋值运算符:
我来这里想知道我是否应该让我的赋值运算符返回一个const或一个非-常量参考。 一些教程 使用非常量版本,这与“Thinking in C++”的建议相反。 一些其他参考文献给出了背后的推理:
I believe OP's example would be suitable to the question if the addition operator is substituted with any other binary operator that returns a reference, for example an assignment operator:
I came here wondering whether I should make my assignment operator return a const or a non-const reference. Some tutorials use non-const versions, contrary to "Thinking in C++"'s advice. And some other references give reasoning behind that: