Should I return bool or const bool?

后端 未结 11 824
我在风中等你
我在风中等你 2021-01-01 10:36

Which is better:

bool MyClass::someQuery() const;

const bool MyClass::someQuery() const;

I\'ve been using \'const bool\' since I\'m sure I

11条回答
  •  有刺的猬
    2021-01-01 11:18

    The const modifier is only used for return types that are returned by reference (either as reference const SomeObject& or via a pointer const SomeObject*), so the caller won't be able to modify the object via the reference/pointer. Primitive types are returned by value, which means that the caller receives a copy of the the object, not the object itself.

    Therefore, const is not really appropriate for returned value types. Since the copy is outside of the control of the called function, the called function should not dictate to the caller that it cannot be changed.

提交回复
热议问题