开发者

Question about 'const' functions, c++

开发者 https://www.devze.com 2023-04-04 11:48 出处:网络
I understand that when I declare a membe开发者_C百科r function as const I actually say that I will not change the class. My question - does \'class\' refer to (*)this instance or to the class in gener

I understand that when I declare a membe开发者_C百科r function as const I actually say that I will not change the class. My question - does 'class' refer to (*)this instance or to the class in general?

For exmaple - if I have a const member function that create a new instance of the same class and edit this instance, is that legal?

thanks!


The const refers to the instance on which you call the function, which is also why static member functions cannot be declared const. A const function can read, but not write, the fields of the this instance, and it can only call static and const methods of this, but it has full access to the global scope.

0

精彩评论

暂无评论...
验证码 换一张
取 消