Effective C + + ITERM3

Source: Internet
Author: User

Article Three

--17/1/16

This article is mainly about the const in C + + applications, after reading really benefited.

(1) First, the Const modifier ensures that the pointer or the pointing object is not const, similar to the constant pointer and pointer constants we see in clause two, which are defined as follows:

T * Const:const pointer,non-const data is what we call a pointer constant;

Const T *:non-const pointer,const Data is what we call a constant pointer;

It is important to note that writing before and after the type is the same, that is, the const t* p and T const*p are the same, are constant pointers, and T is the immutable object.

The pointers in the STL also have a similar form:

A. Declare a pointer to an iterator that refers to T*const, which is not allowed to change the address pointed to by the pointer, as follows:

Const Std::map<int,int>::iterator IT = Mp.begin ();

B. Want to iterate only to iterate over the desired content, not allow to modify the value inside the specific container, that is, the const t* is required by the STL to provide const_iterator, the following wording:

Std::map<int,int>::const_iterator it = Mp.begin ();

The above is an important application of const in pointers and iterators.

(2) The application of const in function declaration is also a widely used and important place to raise attention. Making the function return a constant value can improve security and efficiency. For example, a sentence like this will be compiled by:

1 classR {2  Public:3 R () {}4 };5 6Roperator* (Constr& L,Constr&r) {7 R t;8   returnT;9 }Ten  One intMain () { A R A, B, C; -(A * b) =C; -   return 0; the}

Although this is a bit bigger than the brain hole, this is an issue that cannot be overlooked, and declaring a function as const can increase security and can be captured by the compiler as a "boring move"?

(3) The application of the const member function is the content that this article spends a lot of time to say, we hope this member function cannot modify the related value of the class, we will add the word of const to this function. Can be overloaded based on const or not, const members call the const function, Non-const calls the Non-const version, which is also mentioned in essential C + + knowledge.

The author then discusses the const member functions of the two factions:

1) Bitwise constness, requires that each bit is constness, that is, the const function is not allowed to have any modification operations, so the compiler is focused on checking a similar assignment operation.

The following code is not the real sense of bitwise constness

1 classR {2 3  Public:4 5RConst Char*p) {6 7str =New Char[Strlen (P) +1];8 9 strcpy (str, p);Ten  One  } A  -  Char&operator[] (int_index)Const { -  the   returnStr[_index]; -  -  } -  +  voidPrint ()Const { -  +printf"%s\n", str); A  at  } -  - Private: -  -   Char*str; -  in }; -  to   +  - intMain () { the  *R R ("Zhenhao"); $ Panax NotoginsengR.print ();//Output Zhenhao -  the   Char*p = &r[0]; +  A*p ='h'; the  +R.print ();//Output Hhenhao -  $   return 0; $  -}

2) So the second is logical constness a const member function can modify the relevant variables inside, that is, logically "immutable" just fine. This is particularly discussed in essential C + +, where the variable that you want to put in the const member function can be changed by using the mutable keyword instead of all variables.

Finally, the author made a summary I think good: The compiler strictly implement bitwise constness, but write the program is should abide by logical constness.

(4) Clever use of conversion to avoid the repetition of const and NON-CONST, when we implement the two versions of the function, if the implementation mechanism makes no difference, then you can use the transformation to avoid this repetition. This avoids the implementation within the member function of the Non-const, converts the Non-const member to Const (secure), and then invokes the corresponding const function to return the value using Const_cast to convert back. Note that it is not possible to convert const to Non-const, because this is not only logically not tenable, but also cumbersome to implement.

Effective C + + ITERM3

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.