Objective C ++ Part1.Accustoming Yourself to C ++

Source: Internet
Author: User

1. View C ++ as a federation of ages

Four languages of C ++:

1) C

2) Object-Oriented C ++

3) Template C ++

4) STL

 

2. Prefer consts, enums, and inlines to # defines

On the one hand, the macro definition is not conducive to locating errors during debugging, and on the other hand, it is mainly caused by the division of labor between the pre-processor and the compiler.

At the same time, macro definitions are too complex and prone to errors.

Summary:

First, for constants, it is best to replace # defines with a const object or enums.

Second, for macro (macros) Like a function, it is best to replace # defines with the inline function.

 

3. Use const whenever possible

Although the const syntax is changeable, It is not unpredictable. If the keyword const appears on the left of the asterisk
Edge, indicating that the indicated object is a constant: If it appears on the right side of the asterisk, it indicates that the pointer itself is a constant. If it appears on
The asterisks on both sides indicate that both the theme and pointer are constants.

Char greeting [] = "Hello ";

Char * p = greeting; // non-const pointer, non-const data

Const char * p = greeting; // non-const pointer, const data

Char * const p = greeting; // const pointer, non-const data

Const char * const p = greeting; // const pointer, const data

If it is a constant, some programmers will write the keyword const before the type, and some will
It is written after the type and before the asterisk. The two methods have the same meaning, so the parameters accepted by the following two functions
The types are the same:
Void f1 (const Widget * pw );
Void f2 (Widget const * pw );
Both forms are used. You should try to get used to them.
The STL selector is modeled based on pointers, so the iterator functions like? Pointer. Voice
The alternative is const, just like declaring a pointer to const (that is, declaring a T * const pointer ).
This iterator cannot point to different things, but the value of the things it refers to can be changed. If you want
Hope that the content referred by the iterator cannot be modified (that is, you want STL to simulate a const T * pointer), you need
Const _ iterator:
Std: vector <int> vec;
Const std: vector <int >:: iterator iter = vec. begin (); // iter acts like a T * const
* Iter = 10; // No problem. Change the things specified by iter.
+ Iter; // error. iter is const.
Std: vector <int >:: const iterator clter = vec. begin (); // cIter acts as a const T *

* Clter = 10; // error, * cIter is const

++ Clter; // No problem

Summary:
First, Something declared as const can help the compiler detect incorrect usage. Const can be applied to objects, function parameters, function return types, and member function bodies in any scope.
Second, the compiler enforces bitwise constness, but you should use "conceptual constants" when writing programs"
(Conceptual constness ).
Third, when the const and non-const member functions have essentially equivalent implementations, enable the non-const version

Use the const version to avoid code duplication.


4. Make sure that objects are initialized before they're used

Static objects are constructed until the end of the program. Therefore, both stack and heap-based objects are excluded. Such objects include global objects, objects defined in the namespace scope, objects declared as static within the classes, within the function, and within the file scope. Static objects in a function are called local static objects (because they are local for the function), and other static objects are called non-local static objects. Static objects are automatically destroyed when the program ends, that is, their destructor are automatically called at the end of main.

The so-called compilation unit <translation unit) refers to the source code that generates a single target file (singleobject file. Basically, it is a single source code file with the header file contained in it (# include files ).

Summary:

First, the internal objects are manually initialized because C ++ does not guarantee their initialization.
Second, it is best to use the member Initial Value column (member initialization list) in the constructor instead of using the value assignment operation (assignment) in the constructor itself ). The member variables listed in the initial value column should be arranged in the same order as their declaration in class.

Third, to avoid the issue of "initialization order of cross-compilation units", replace the non-local static object with a local static object.


Perception:

From this part of the content, we can roughly see the style of the entire book, mainly to tell you which situation is more suitable and can avoid errors in a variety of situations that can be implemented. In this way, the reader's level will go from the general understanding of C ++ to a deeper level. The listed points are common. You must have some practical experience in C ++ to read the results. Otherwise, you cannot remember the results. It is an effect for people with few practical experiences to directly read the design model.

 

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.