"Effective C + +" Reading notes 03: Versatile const

Source: Internet
Author: User
Tags bool modifier

Const is a regular customer when we write C + + code, and for those objects that we don't want to modify, it's best to use the const modifier.

1. Let's take a look at some idiomatic methods:

1 char greeting[] = "Hello";
2 char* p = greeting;             //非const指针,非const数据
3 const char* p = greeting;       //非const指针,const数据
4 char const *p = greeting;       //非const指针,const数据
5 char* const p = greeting;       //const指针,非const数据
6 const char* const p = greeting; //const指针,const数据

Lines 3 and 4 are different in form, but the functionality is similar.

3 and 5 are essentially different, so to understand: const modifies all the code behind it, such as the const modifier char* p in 3rd, 4, and the char*, and in 5th, the const modifier p, which represents the pointer.

But these are different in STL, because iterator is a pointer, modifying a iterator with Const is similar to line 3rd above, and if you want to produce the effect of line 5th, you need to use Const_iterator:

1 using namespace std;
 2
 3 vector<int> vec;
 4 const vector<int>::iterator iter = vec.begin();//类似于T*  const
 5 *iter = 10; //正确,可以修改iter所指的值
 6 ++iter; //错误,iter是const
 7
 8 vector<int>::const_iterator cIter = vec.begin();//类似于const  T*
 9 *cIter = 10; //错误,cIter指向的值是const
10 ++cIter; //正确

To sum up: In a general application, const modifies subsequent variables and modifiers, and only in the STL iterator, the const iterator==t* const;const_iterator==const t*.

Remember, as much as possible, to declare variables that cannot be modified as const!

2. When you use const in a member function, if you declare a member function as const, any modification to any member variable in the function will result in an error that prevents us from unintentionally modifying the object

1 class TestBlock
 2 {
 3   public:
 4     void ConstFunc() const;//const函数
 5   private:
 6     char* text;
 7     int size;
 8     bool isValid;
 9 }
10
11 void TestBlock::ConstFunc() const
12 {
13   if(!isValid)
14   {
15     size = strlen(text);//错误
16     isValid = true;//错误
17   }
18 }

You can eliminate this error by declaring the member variable to be mutable (mutable)

1 mutable int size;
2 mutable bool isValid;
3

3.const can be used for overloading, if two function parameters are identical, you can overload the return value by declaring it as const or declaring the function as const, but when implemented, the const function can be invoked with a non-const function to reduce code duplication. This piece of feeling is not too much, for the time being.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.