const-method

Why is std::basic_string::operator[] a const method if it's also a non-const method?

我们两清 提交于 2020-01-07 02:44:17
问题 http://cplusplus.com/reference/string/basic_string/operator[] I understand that it's advantageous to have a second version which returns const to prevent warnings when a const result is required and to mitigate casting but if the function already provides a non- const method (method-- not result) then what is the point of declaring the const -result method const ? 回答1: You need to understand, that the second ( const ) version not only returns a different result but is also marked itself as

Should I declare these methods const?

给你一囗甜甜゛ 提交于 2019-12-12 08:22:16
问题 I'm working on some C++ code where I have several manager objects with private methods such as void NotifyFooUpdated(); which call the OnFooUpdated() method on the listeners of this object. Note that they don't modify the state of this object, so they could technically be made const methods, even though they typically modify the state of the system as a whole. In particular, the listener objects might call back into this object and modify it. Personally I'd like to leave them as they are and

Should I declare these methods const?

北城余情 提交于 2019-12-04 00:10:51
I'm working on some C++ code where I have several manager objects with private methods such as void NotifyFooUpdated(); which call the OnFooUpdated() method on the listeners of this object. Note that they don't modify the state of this object, so they could technically be made const methods, even though they typically modify the state of the system as a whole. In particular, the listener objects might call back into this object and modify it. Personally I'd like to leave them as they are and not declare them const . However, our static code checker QAC flags this as a deviation, so I either

C++ overload resolution, conversion operators and const

不问归期 提交于 2019-12-01 20:23:34
问题 In this case void f(int *); void f(const int *); ... int i; f(&i); the situation is pretty clear - f(int *) gets called which seems right. However, if I have this (it was done like that by mistake(*) ): class aa { public: operator bool() const; operator char *(); }; void func(bool); aa a; func(a); operator char *() gets called. I cannot figure out why would such decision path be better than going to operator bool(). Any ideas? (*) If const is added to the second operator, the code works as

C++ - Why static member function can't be created with 'const' qualifier

自古美人都是妖i 提交于 2019-11-28 02:48:17
Today I got a problem. I am in the need of a static member function, const is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how? When you apply the const qualifier to a nonstatic member function, it affects the this pointer. For a const-qualified member function of class C , the this pointer is of type C const* , whereas for a member function that is not const-qualified, the this pointer is of type C* . A static member function does not have a this pointer (such a function is not called on a particular instance of a class), so const qualification of a

C++ - Why static member function can't be created with 'const' qualifier

末鹿安然 提交于 2019-11-26 18:47:13
问题 Today I got a problem. I am in the need of a static member function, const is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how? 回答1: When you apply the const qualifier to a nonstatic member function, it affects the this pointer. For a const-qualified member function of class C , the this pointer is of type C const* , whereas for a member function that is not const-qualified, the this pointer is of type C* . A static member function does not have a this