Clause 3: Use const as much as possible (using const whenever possible)

Source: Internet
Author: User

1. As long as this (a value remains the same) is true, it should be really said, so that you can get the help of the compiler to ensure that this constraint is not violated.

2.keyword const can be used in many ways, but it is easy to use.

2.1classes externally modifies constants in the Global/namespace scope.

2.2 Modifies a static object declared in a file, function, or chunk scope.

2.3 Modifies the static and Non-static member variables inside the classes.

2.4 For pointers, you can indicate that PTR itself, PTR, or both (or neither) are const.

There is no clear static and const relationship??? Is it generally not static vs. dynamic?

eg

Char greeting[] = "Hello";

char* p = greeting; Non-const Pointer,non-const Data

const char * p = greeting; Non-const Pointer,const Data

char* const P = greeeting; Const pointer, non-const data

const char * Const P = greeting; const pointer, const data

Char const * Const P = greeting; const pointer, const data

void F1 (const widget * PR);

void F2 (Widget const * PR);//Both are referred to as const

By example, it is possible to distinguish when the object is represented and the pointer itself is a constant.

3.STL iterators:

This is not too understand, before the STL know less???

4. function declaration When applied:

A const can be associated with a function return value, each parameter, and the function itself.

The 4.1const function returns a value that reduces the surprises of low-level errors and does not affect security and efficiency.

A*b = c;//attempts to change the result of the * operator and then forcibly assigns a value of C.

if (a*b=c);//write = =

4.2const member functions

Confirm that the member function can be used for const objects.

4.2.1 can quickly understand that member functions can alter object content.

4.2.2 makes it possible for a function to manipulate a const object.

Two member functions can be overloaded if they are just constants (constness). This feature is described in a large number of paragraphs.

Both const and constness functions with the same name exist in a class. Then the const object call is const member function. Constness Object call constness member function.

4.2.3 Bitwise const (physical constness) and logic constness

This concludes with a workaround: mutable (variable). This quickly sweep again, understand, feel nothing. But forget it again???

5.casting (transformation)

This is not too familiar, static-cast/const-cast/dynamic-cast ...

The const member function does not change the logical state of its object (logic states), and the Noo-const member function does not have this commitment.

Note:

1. Declaring something as const can help the compiler detect the wrong usage. A const can be applied to any scope object, function parameter, function return value, member function body.

2. The compiler enforces bitwise constness, but you should write a program that uses "conceptual constants" (conceptual Constness) (which has been forgotten, although it is understood!!!). )

3. When the const and NON-CONST member functions have an equivalent implementation, making the NON-CONST version call the const version avoids code duplication. (This is actually the function of overloading it!!) )

Clause 3: Use const as much as possible (using const whenever possible)

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.