Effective c++--55 specific practices for improving procedures and design (iii)

Source: Internet
Author: User
Tags bitwise constant definition

Article 01: Treat C + + as a language federal

C + + = C (c basic syntax) + object-oriented C + + (class, encapsulation, inheritance, polymorphism ...) + Template C + + (Generic programming) + STL (container, iterator, algorithm, function object).

clause 02: Replace # define as const,enum,inline as possible

1. #define is not considered as part of the language, its defined symbols are not added to the symbol table, the macro cannot set breakpoints, can not be tracked debugging;

2. #define Define a macro that resembles a function (instead of defining a constant), even if you have already joined the parentheses for all of the macros in the macro , you can still get into trouble (for example, the parameter is the ++i operator).

So:

1. For simple constants, it is best to replace #defines with const objects or enums;

2. For macros that resemble functions (macros), it is best to replace #defines with the inline function instead.

Attention:

1. You cannot create a class-specific constant with # define (of course, the macro does not provide any encapsulation ) because #defines does not valuescope. Once a macro is defined, He will be valid in the subsequent compilation process (unless it is #undef somewhere).

2. To define the exclusive constants of a class, for example, some compilers do not allow static members (defined as static, to ensure that at most one of the constants) are given initial values on their declarations (usually in the header file), and so-called "In-class Initial value Settings" Allow only integer constants. As follows:

classcostestimate{Private:       Static Const DoubleFudgefactor;//static class constant declaration, located in the header file (. h)       .......             enum{Numturns =5};//enumeration variables, which are used to provide the necessary constants in class declarations that require an initial value setting       intScores[numturns];//using enumeration Constants};Const DoubleCostestimate:fudgefactor =1.35;//static class constant definition, located within the implementation file (. cpp)

You can use an enumeration value to compensate for the "in class initial value setting", which is based on the theory that "a value of enum type can be used INTs". Enumeration types behave in a way that is more like a # define than a const. For example, taking a const address is legal, but taking an enum address is illegal, and taking a # define address is often illegal.

Clause 03: Use const whenever possible

const allows you to specify a semantic constraint, and the compiler enforces this constraint.

1. The const modifier pointer, as follows:

CharGreeting[] ="Hello";Char*p = greeting;//non-const pointer,non-const DataConst Char*p1 = greeting;//equivalent to "char const * p1 = greeting;", Non-const pointer,const dataChar*ConstP2 = greeting;//Const POINTER,NON-CONST DataChar*Const ConstP3 = greeting;//Const POINTER,CONST Data

Note: for the P1 pointer, the object is const data, but this is only for the P1 pointer is const data, if at this time with another normal pointer ptr also points to P1 point, you can use the PTR pointer to modify the point of the object.

2. Const member functions

The purpose of applying a const to a member function is to confirm that the member function is available for use with a const object.

The Bitwist Constness Camp believes that a member function can be said to be const only if you do not change any of the member variables of the object (except static). But there is an exception: specifically, a member function that changes the "pointer" does not count as const, but if only the pointer (not its object) is subordinate to the objects, it can be modified by PTR by defining a normal pointer, pointing to the pointer to a member of the class, and then by using the reference ( This is the case described in the previous point), which leads to anti-visual results: We define the intent of const not to modify the object. As follows:

classCtextbook { Public:    ...    Char&operator[] (std::size_t position)Const{returnptext[position];}Private:    Char* PTEXT;//only the pointer is subordinate to the object};ConstCtextbook CCTB ("Hello");//declares a constant objectChar* PC = &cctb[0];//Call Const operator[] to get a pointer to the CCTB data*PC ='J';//CCTB now has something like "jello."

If you need to modify the values of some non-static member variables within a const member function, you can add a const-related oscillating field to this member variable:mutable(variable). Mutable releases the bitwise constness constraint for the non-static member variable.

3. Avoid duplication in const and NON-CONST member functions

When the const and NON-CONST member functions have a substantially equivalent implementation , making the Non-const version call the const version avoids code duplication. As follows:

Class textbook{ Public:   .....Const Char&operator[] (std::size_t position)Const    {        .....        .....        returnText[position]; }Char&operator[] (std::size_t position) {returnconst_cast<Char&> (//const removal of op[] return value (constant removal)static_cast<ConstTextbook&> (* This)//Add a const to the *this to enable it to be called to the const op[][position]); }}

So:
1. Declaring something as const can help the compiler detect incorrect usage. A const can be applied to objects, function arguments, function return types, and member function bodies within any scope.

2. The compiler enforces bitwise constness, but you should use "Conceptual constants" (conceptual constness) when you write your program.

3. When the const and NON-CONST member functions have a substantially equivalent implementation, making the NON-CONST version call the const version avoids code duplication .

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Water and electricity

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Article 01: Treat C + + as a language federal

Effective c++--55 specific practices for improving procedures and design (iii)

Related Article

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.