开发者

Simple question about C++ constant syntax

开发者 https://www.devze.com 2022-12-28 11:58 出处:网络
Here is some code copied from Thinking in C++ Vol1 Chapter 10. #include <iostream> using namespace std;

Here is some code copied from Thinking in C++ Vol1 Chapter 10.

   #include <iostream>
   using namespace std;

   int x = 100;

   class WithStatic {
        static int x;
        static int y;
        public:
             void 开发者_如何学Cprint() const {
             cout << "WithStatic::x = " << x << endl;
             cout << "WithStatic::y = " << y << endl;
           }
  };

what's the meaning of const for the function print()? Thanks!


I've heard this described previously as “a method that does not logically change the object”. It means that by calling this method the caller can expect the object’s state to remain the same after the method returns. Effectively, the this pointer becomes a constant pointer to a constant instance of that class, so member variables cannot be altered. The exception to this rule is if member variables are declared with mutable. If a class has mutable member variables, these can be modified by both non-const and const methods. Also, non-const methods cannot be called from within a const method.

Some people use mutable member variables to cache results of timely computations. In theory, the state of the object does not change (i.e. the only effect is that subsequent calls are quicker, but they produce the same results given the same input).


It means that it doesn't change any member variables of the class.

http://www.parashift.com/c++-faq-lite/const-correctness.html


It's a guarantee that the function won't modify the object at all (i.e. it's a "read-only" function). EDIT: Apparently, the exception to that rule is if an object has mutable members; those can be changed by both const and non-const functions.

Also, I'm glad to see somebody else learning from TIC++. It's a great resource for beginners.


If a member function is declared "const", it means that the function will not modify the object's state. This:

  • Documents the fact that the function will not modify state (useful to other developers).
  • Allows the compiler to make optimizations, knowing that the result of other "const" functions will not have changed as a result of calling that function (which allows function calls to be taken out of loops).
  • Enables one to call the function when the object is passed by const reference, const pointer, etc.

For all the above reasons, one should, as a general rule of thumb, declare a function as "const" if it does not logically modify the state of the object. If the logical state of the object changes, then don't use "const". As an aside, there are cases where the actual state changes but the logical state does not (e.g. caching), in which case one should still mark the function as "const", but one needs to use the "mutable" keyword with the caching variables in order to tell the compiler that modifying them actually doesn't change the logical state.


What it does is effectively make the this pointer be a const pointer to const instead of a const pointer to non-const. So, any time that you reference this in a const member function - either explicitly or implicitly - you're using a const pointer to const.

So, in the case of the class you have here, in any non-const function, the type of this is WithStatic const * while in the const functions, its type is const WithStatic * const.

Like with any pointer to const, you can't alter anything that it points to. So, you can't alter any of its member variables, and you can't call any of its non-const member functions.

Generally speaking, it's a good idea to make a member function be const if you can reasonably do so, because it guarantees that you're not going to alter the object's state, and you can call it with a const object.

It is possible for member variables to be altered if they're mutable or volatile, but those are more advanced topics that are probably better avoided until you're more familiar with the language. Certainly, you don't usually need to worry about them and shouldn't use them unless you need to. It's also possible to cast away the constness of the this pointer, at which point you could alter it, but IIRC, that's undefined behavior, and it's definitely generally considered a bad idea. So, there are instances where it's possible to alter an object's state within a const member function, but it's not normally possible and best avoided even when it is.

When you make a member function const, you're effectively promising that the object's state will not be changed by that function call (though there can obviously be side effects as evidenced by the fact that you can call functions like printf()).


Intuitively, it means that the method doesn't modify the object on which it is called. But really, it means two things:

  • The method may be called on an object or object pointer declared as const.
  • Within the method, the this pointer becomes const. Consequently, all members (except those declared mutable) are const, i.e. read-only, within the context of the method. However, it may still modify:
    • data pointed to by members of the object it's called on
    • data accessed other than through the this pointer — including global data, data pointed to by the method's parameters, static members of the class, other objects of the class, even the same object (if it happens to have access to a non-const pointer to it or bypasses constancy using a const_cast).


the const means print() is not allowed to modify state variables not marked with mutable.

0

精彩评论

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

关注公众号