Effective C + + 01 make yourself accustomed to C + +

Source: Internet
Author: User

article 01: treat C + + as a language federal

to better understand C + +, we decompose C + + into four major sub-languages:

    • C in the final analysis C + + is still based on C. chunks, statements, pre-processor, built-in data types, arrays, pointers all from C.
    • object-oreinted C + + This part is the most direct implementation of the classical code of object-oriented design in C + +. classes, encapsulation, inheritance, polymorphism, virtual functions, etc...
    • Template C + + This is the C + + generic programming Section.
    • STL STL is a template program Library. Containers (containers), iterators (iterators), algorithms (algorithms), and Function objects (functions objects) ...

please remember:

    • These four secondary languages, when you switch from one sub-language to another, lead to an efficient programming code that requires you to change the Strategy. The code for efficient programming of C + + varies depending on what part of C + + you Use.

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

This clause may be changed to "rather than replacing the preprocessor with a compiler." That is, try to use less preprocessing.

Compilation Process:. C file--preprocessing-->.i file--compiling-->.o file--link-->bin file

The preprocessing process scans the source code, makes a preliminary conversion, and generates a new source for the Compiler. Examine the statements and macro definitions that contain the preprocessing directives, and convert the source code accordingly. The preprocessing process also removes comments and extra white-space characters from the Program. The preprocessing process can be seen before the source code is processed by the Compiler. A preprocessing directive is a line of code that begins with a # Number.

Example: #define Aspect_ratio 1.653

The token name Aspect_ratio may have never been seen by the compiler, and it may have been removed by the preprocessor before the compiler started processing the source Code.

That is, Aspect_ratio has been replaced by 1.653 when compiling the source Code. The aspect_ratio may not have entered the tick list (symbol table).

Replace: Const Double Aspectratio = 1.653;

The benefits are: more type checking, because # define is a simple substitution, and this substitution can occur in multiple copies in the target code 1.653; the use of constants never happens the Same.

Constants Replace # two points note:

    • To define a constant pointer:
const char *authorname = "shenzi"; cosnt std::string authorname ("shenzi");

Class-specific constants:

static const int numturns = 5;//static constant All objects have only one copy.

In case you compiler does not allow the "static integer class constant" to complete "in calss initial value setting" (that is, Setting the initial value of the static shaping in the class declaration),

We can compensate by enumeration type:

Enum {numturns = 5};

* It is legal to take a const address, but it is illegal to take an enum address . It is often illegal to take a # define Address.

If you don't want someone to get an integer constant that pointer or reference points to you, an enum can help you implement that Constraint.

Cases:

#define CALL_WITH_MAX (a b)    F ((a) > (b))? (a): (b))

A macro looks like a function, but it does not incur the overhead of a function call, but rather a simple substitution.

Replace:

Template<typename t>inline void Callwithmax (cosnt t &a, cosnt t &b) {f (a > b? a:b);}

Callwithmax is a real function that follows the rules for use and access.

Please remember:

      • For simple constants, It is better to replace #defines with const objects or enums;

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

clause 03: use const whenever possible

The const allows you to tell the compiler and other programmers that a value should remain the same, as long as "a certain value" really should not be changed, it should be said.

example: char greeting[] = "Hello"; char *p = greeting;    Both the pointer p and the string referred to can be changed; const char *p = greeting;    The pointer p itself can be changed, such as P = &anyother;p refers to a string that cannot be changed; char * cosnt p = greeting;    Pointer p cannot be changed, the object can be changed; const char * Const P = greeting;    The pointer p and the resulting object cannot be changed;

Description

    • If the keyword const appears to the left of the asterisk, it represents the therein constant that is referred to. the const char *p and Char const *p have the same meaning as the two notation, which indicates that the resulting object is constant;
    • If the keyword const appears to the right of the asterisk, it indicates that the pointer itself is a constant.

The iterator acts like a t* pointer;

Iterator: iterators cannot be changed, but the values they refer to can be changed;

Const_iterator: the object can not be changed;

STL example: Const Std::vector<int>::interator ITER = Vec.begin (); *iter =10;  No problem ++iter; Error: iter is conststd::vector<int>::const_iterator citer = Vec.begin ();//function like const T*,*citer = 10//error: *citer is const  ++citer; No problem;

please remember:

    • Declaring something as const can help the compiler detect the error usage. Const can be applied to objects in any scope, function parameters, function return types, member function ontology;
    • The compiler enforces bitwise constness, But you should use "conceptual vehicles" (conceptual constness) when you write Programs.
    • When the cosnt and NON-CONST member functions have a substantially equivalent implementation, making the Non-const version call the const version avoids code Duplication.
article 04: determine that the object was initialized before it was used

Always initialize an object before it is Used. For built-in types without any members, you must do this manually.

For anything other than the built-in type, the initialization responsibility falls on the constructor, ensuring that each constructor initializes each member of the Object.

please remember:

    • Manual initialization of built-in objects, because C + + does not guarantee initialization of them;
    • Constructors are best used with member initialization lists, rather than using assignment operations within the constructor Body. The initialization list of member variables should be in the same order as they are declared in the class;
    • To exempt from the initialization order of Cross-compilation units, replace the non-local static object with the local static Object.

Effective C + + 01 make yourself accustomed to C + +

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.