[英]Can I have a const member function that returns *this and works with non-constant objects?
If I understand correctly, a member function that is not supposed to modify the object should be declared as const
to let the users know about the guarantee. 如果我理解正确,应该将不应该修改对象的成员函数声明为const
以便让用户知道保证。 Now, what happens when that member function returns the reference to *this
? 现在,当该成员函数返回对*this
的引用时会发生什么? For example: 例如:
class C{
public:
C &f() const {return *this;}
};
Inside C::f()
, this
has the type const C*
, so, the following will not compile: 在C::f()
, this
的类型为const C*
,因此,以下内容不会编译:
int main() {
C c; // non-constant object!
c.f();
return 0;
}
Of course, we could provide a non-const version of C::f()
to work on non-constant objects: 当然,我们可以提供非常量版本的C::f()
来处理非常量对象:
class C{
public:
const C &f() const;
C &f();
};
However, I do not believe that this is what we want. 但是,我不相信这是我们想要的。 Note that the non-constant version does not change the object, but this promise to the users is not expressed... Am I missing something? 请注意,非常量版本不会更改对象,但是对用户的这种承诺没有表达出来......我错过了什么?
EDIT: Let me just summarize the question for clarity: f()
does not modify the object on which it is called, so declaring it as C &f();
编辑:为了清楚起见,我只是总结一下这个问题: f()
不修改调用它的对象,因此将其声明为C &f();
without making it a const
member is misleading. 不使它成为const
成员是误导。 On the other hand, I do want to be able to call f()
on non-const objects. 另一方面,我确实希望能够在非const对象上调用f()
。 How do I resolve this situation? 我该如何解决这种情况?
EDIT: It comes out from all the discussion that took place in the comments that the question was based on an incorrect understanding of what const
ness of a function member implies. 编辑:它来自评论中发生的所有讨论,该问题是基于对函数成员的const
所暗示的错误理解。 The correct understanding that I am taking away for myself is: 我正在为自己带走的正确理解是:
A member function that returns a non-const reference is intended to allow its users to change the object through the returned reference. 返回非const引用的成员函数旨在允许其用户通过返回的引用更改对象。 Therefore, even though this function does not change the object by itself, there should be no inclination to declare it to be a const
member! 因此,即使此函数本身不更改对象,也不应该将它声明为const
成员!
Your problem is the original function definition: 您的问题是原始函数定义:
C &f() const {return *this;}
here you return a non-const reference to the const object, which would allow changing the const object and would be dangerous, therefore it's forbidden. 在这里你返回一个对const对象的非const引用,这将允许更改const对象并且是危险的,因此它是被禁止的。
If you were to write it as 如果你把它写成
const C &f() const {return *this;}
would be callable from both const and non-const objects and would always return a const reference. 可以从const和非const对象调用,并且总是返回一个const引用。
Of course, we could provide a non-const version of C::f() to work on non-constant objects. 当然,我们可以提供一个非常量版本的C :: f()来处理非常量对象。 However, I do not believe that this is what we want. 但是,我不相信这是我们想要的。
Probably that is exactly what you want. 可能这正是你想要的。 It's the only way to return a non-const reference when called on non-const objects and keep const correctness when calling it on const objects. 它是在非const对象上调用时返回非const引用的唯一方法,并且在const对象上调用const时保持const正确性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.