C ++ programming ideology (Volume 1) read and summarize, programming ideology volume 1

Source: Internet
Author: User

C ++ programming ideology (Volume 1) read and summarize, programming ideology volume 1

I recently went to work idle and read the section "C ++ programming ideas" I think is the most helpful part for me. I will leave it for my own review, on the other hand, I will share with my friends:

1 OPP Problems

1.1. Each object has a type. In OOP, class and type are synonyms. In object-oriented programming, what we do is actually to create new data types, programmers define classes to adapt to specific problems, rather than being forced to apply existing data types.

1.2 When <> is used to specify a file, the pre-processor looks for the file in a specific way, which is generally a search path specified by the environment or compiler command; when "" is used to specify a file, it is usually searched from the current directory. If the file is not found, search by <>.

2 C ++ total C

2.1. the integer type ranges from small to large: short int-> long int.

Float-> double-> long double;

The number of unsigned digits does not save symbols, so there is an extra bit available, so it can store two times larger than the number of signed integers

2.2. the internal and external variables of main () are stored in different regions. Data and functions also exist in different regions.

2.3 The general principle of C ++ programming is initialization at definition, and the const variable must have an initial value.

2.4. enum can improve the definition of the program. Using union can save memory. All union variable addresses are the same.

2.5 use a pre-processor to complete code debugging. The specific implementation is as follows:

#define NDEBUG //......#ifdef NDEBUG/* debugging code here*/
......#endif
2.6. C ++ allows you to assign any type of pointer to void *, but it does not allow you to assign the void * z pointer to any other type of pointer.

2.7. The same file is a contract between us and the database users. The contract describes the data structure in the database and specifies parameters and return values for function calls. The header file is only limited to declarations, to avoid repeated declarations of the header file content, we can avoid them as follows:

#ifndef HEADER_FLAG#define HEADER_FLAG//Type declaration here.....#endif
Similarly, to avoid namespace confusion, the header file will never see using

2.8 If a programmer wants to allow an object that does not belong to the current result to access the current structure explicitly, he can use the friend modifier, which must be declared inside a structure.

3. C ++ features:

3.1 The difference between class and struct is that the default class member is private, while the default struct member is public;

3.2. The security of C ++ is embodied in initialization and clearing. The constructor is used to ensure initialization and the Destructor is used to ensure cleanup.

3.3. The Macros in C ++ are replaced by inline functions, which ensures high efficiency (the member functions and Preprocessor macros are efficient) security is also ensured (when calling an inline function, the compiler first ensures that the call is correct, that is, all parameter types are satisfied, and the macro definition is in preprocessing and cannot be checked ).

Any function defined in the class automatically becomes an inline function;

The purpose of using inline functions is to reduce the pleasure of function calling. However, if a function is too large, the code will expand because you need to repeat the value of the Code in every part of the function to be called, therefore, it is ideal to use small functions as inline functions.

3.4. static stores the variables in the static data zone instead of the stack. The constructor of the Global static object is called before main () and calls the Destructor after exiting main;

3.5. How do the key words namespace put their member names in different spaces like class, struct, enum, and union? However, namespace has obvious differences with class, struct, union, and enum:

(1) namespace can only be defined globally, but they can be nested with each other;

(2) The End Of The namespace definition does not need to be followed by a semicolon;

(3) a namespace can be defined by an identifier in a multi-head file;

(4) You cannot create an instance named like a class;

(5) Another namespace name can be used as its alias;

3.6. In C ++, reference can be understood as a fancy pointer. The advantage of this pointer is that it does not have to be suspected of initialization. The reference rules are as follows:

(1) Application creation must be initialized;

(2) A reference pointing to an object cannot be converted into a reference of another object;

(3) References that cannot be NULL

The most common reference is the function parameter and return value. When the reference is used as a function parameter, any modification to the referenced object in the function will change outside the function, of course, pointers can be passed to do the same thing, but references have clearer syntax.

3.7 copy-constructor is a special constructor. It must be referenced to generate new objects from existing objects of the same type. The compiler uses the copy constructor to pass and return objects in the function by passing values.

3.8 using delete void * may cause errors. You must convert it to an appropriate type before deletion; otherwise, the memory will be lost;

3.9. C ++ ensure that other constructor of the shortening is called before the constructor body of the new class, in this way, any call made by the member functions of all sub-objects is always transferred to the initialized object,

MyType::MyType(int i):Bar(i){///....}
4. polymorphism in C ++

4.1 polymorphism always occurs between the derived class and the base class. C ++ is implemented through the virtual function and late binding;

If a function is declared as virtual in the base class, it is virtual in all derived classes and can be overwritten;

In each class with a virtual function, the compiler secretly places a pointer (vpointer) pointing to the VTABLE of the object (the compiler creates a table for each class containing the virtual function: VTABLE)


The compiler starts from the Instrument pointer and points to the starting address of the object. For all instruments or derived objects, their vptr has the same position (often at the beginning of the object ), therefore, the compiler can retrieve the vptr of the object, and the vptr points to the starting address of the vtable. All vtables have the same order ,. Play () First, what () 2nd;

5. C ++ template)

5.1. template tells the compiler that the subsequent definition will make one or more unspecified types wrong. When this template generates actual class code, you must specify these types so that the compiler can replace them.



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.