简体   繁体   English

我可以使用const成员函数返回* this并使用非常量对象吗?

[英]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.

相关问题 为什么非常量静态成员有多个定义? - Why would a non-constant static member have multiple definitions? 创建指向成员函数的非恒定指针以进行SDL事件过滤 - Create a non-constant pointer to member function for SDL event filtering 为什么可以在常量结构上调用非const成员函数? - Why can a non-const member function be invoked on a constant structure? 我可以为 class 成员使用非常量数组大小吗? - Can I use non-constant array size for class members? 当field为const时,将'非常量表达式作为数组绑定' - Getting 'Non-constant expression as array bound' when field is const 我可以在C ++中声明一个非成员函数const吗? - Can I declare a non-member function const in C++? 为什么我不能将 class 的常量实例与相同 class 的非常量实例进行比较? - Why can't I compare a constant instance of a class with a non-constant instance of the same class? 非常数数组 - Non-Constant Array 我只能使用const重载运算符成员函数吗? - Can I only have const overloaded operator member function? C++ constexpr 函数实际上可以接受非常量表达式作为参数吗? - Can C++ constexpr function actually accept non-constant expression as argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM