《Effective C++》读书笔记之三 Item 3. Use const whenever possible

一. Pointer      
     1. If the const appears to the left of the asterisk(*), what‘s pointed to is constant;
     2. If the const appears to the right of the asterisk(*), the pointer itself is constant;
     3. If the const appears on both sides, both is constant.    
     eg:
     char *greeting = "hello world";
     const char *p = greeting;     // data is const
     char * const pp = greeting;    // pointer is const
     const char * const bp = greeting; // both is const     
     p++;   *p = ‘K‘; 
     pp++;  *pp = ‘K‘;     
     " const char *p " is equal to "char const *p";
   
二. Iterator
     STL iterators are modeled on pointers, so an iterator acts much like a T* pointer.    
     If you want an iterator that points to something that can‘t be modified , you want a const_iterator .
     eg:
          std::vector<int> vec;
          const std::vector<int>::iterator iter = vec.begin();  // iter acts like a T* const; 
          *iter = 10;  ++iter; 
          std::vector<int>::const_iterator cIter = vec.begin(); // iter acts like a const T*;
          *cIter = 10; ++cIter;

三. function
     Some of the most powerful uses of const stem from its application to function declarations. Within a function declaration, const can refer to the function‘s return value, to individual parameters, and, for member functions, to the function as a whole.     
     1. function‘s return value
          Having a function return a constant value often makes it possible to reduce the incidence of client errors without givin g up safety or efficiency.      
     2. individual parameters
          There‘s nothing particularly new about const parameters — they act just like local const objects, and you should use bot h whenever you can. Unless you need to be able to modify a parameter or local object, be sure to declare it  const.

四. const member function
     The purpose of const on member functions is to identify which member functions may be invoked on const objects. Such member functions are important for two reasons. First, they make the interface of a class easier to understand. It‘s important to know which functions may modify an object and which may not. Second, they make it possible to work with const objects.
     One of the fundamental ways to improve a C++ program‘s performance is to pass objects by reference- to- const.
     class string{
          public:
               ...
               const char& operator[ ](std::size_t index) const {
                    return data[index];
               }

               char& operator[ ]( std::size_t index) {
                    return data[index];
               }
          private:
               char* data;
     };

     f(const string& str){
          str[0];  // call const function 
          str[0] = ‘p‘;  // wrong, cannot modify
     }

     string nstr("sfsd");
     nstr[0]; // call non-const function
     nstr[0] = ‘o‘; // Ok.
     What does it mean for a me mber function to be const? There are two prevailing notions: bitwise constness  (also known as physical constness) and logical constness.
     Const member function isn‘t allowed to modify any of the non- static data members of the object on which it is invoked.
     Avoiding Duplication in const and Non-const Member Functions.
     char& operator[ ]( std::size_t index){
          return  
          const_cast<char&>
          static_cast<const string&>(*this)[index];
     }

五. Things to Remember:
     Declaring something const  helps compilers detect usage errors. const can be applied to objects at any scope, to function parameters and return types, and to member functions as a whole.
     Compilers enforce bitwise constness, but you should program using conceptual constness.
     When const and  non-const  member functions have essentially identical implementations, code duplication can be avoided by having the non- const version call the const  version.

《Effective C++》读书笔记之三 Item 3. Use const whenever possible,布布扣,bubuko.com

《Effective C++》读书笔记之三 Item 3. Use const whenever possible

上一篇:Javascript 学习 笔记四


下一篇:用C++类模板实现栈结构出现的问题以及思考