A comparison between const and # define
1, const constants have data types, and macro constants do not have data types. The compiler can perform a type check on a const
Check. A macro constant is just a character substitution and can produce unexpected errors.
2. Some integrated debugging tools can debug const constants, but cannot debug macro constants.
The enumeration type is used when constant constants are required within a class.
If the input parameter passes the object as a value, it is advisable to use the "const&" method to pass it, which can
Eliminate the construction and destruction of temporary objects, thus improving efficiency.
Assert is a macro that works under Debug and detects situations where an error should not occur.
Rules to refer to:
1, the reference is created must also be initialized (the pointer can be initialized at any time)
2. Cannot use a null reference, the reference must be associated with a valid storage unit (the pointer can be NULL)
3, once the reference is initialized, you cannot change the reference relationship (the pointer can change the object at any time)
There are three ways to allocate memory
1, from the static storage area allocation. The memory is allocated at the time of program compilation, the whole
exist during a run. such as global variables, static variables.
2, create on the stack. When executing a function, the storage unit of the local variable within the function can be created on the stack, and the function
These storage units are automatically freed at the end of execution. The stack memory allocation operation is built into the processor's instruction set,
Rate is high, but the allocated memory is limited.
3, allocated from the heap, also known as dynamic memory allocation. When the program is running, use malloc or new to apply any number of
Memory, the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by our
Decision, the use is very flexible, but the problem is the most.
Comparison of pointers to arrays
Arrays are either created in the static store or created on the stack. The array name corresponds to (instead of pointing to) a
Block memory, whose address and capacity remain constant over the lifetime, can be changed only with the contents of the array.
The pointer looks at any type of block of memory at any point, its characteristics are "variable", so we use pointers to manipulate
Dynamic memory. Pointers are far more flexible than arrays, but they are also more dangerous.
Two main causes of "wild pointer"
1. The pointer variable is not initialized. Any pointer variable that has just been created does not automatically become a null pointer, its
The default value is random, and it can be arbitrary. Therefore, pointer variables should be initialized at the same time they are created, or
Set the pointer to null, or point it to legitimate memory.
2, after the pointer is free or delete, is not set to NULL, the person is mistaken for a legitimate pointer
The difference between malloc and new
For objects of non-intrinsic data types, the light malloc is not sufficient to satisfy the requirements of a dynamic object. Object in the creation
constructor is automatically executed, but malloc is a library function, not an operator, not a compiler control
permissions, the task of executing the constructor cannot be imposed with malloc. The type of the MALOOC return value is a
(void*) pointer.
New is an operator, and the type of the return value is the type of the new data.
For C-language functions, C + + adds new mechanisms for overloading, inline, const, and virtual four. where Overloads and
The inline mechanism can be used in both global hasn ' t or member functions of the class, and the const and virtual mechanisms use only
To the member function of the class.
Overloaded functions: Scopes must be the same, and can only be distinguished by the different values of the arguments.
Overloading and overwriting
Features that are overloaded by member functions:
1, the same range (in a class);
2, the function name is the same;
3, the parameters are different;
4, virtual key words are optional;
Overrides refer to a derived class function overriding a base class function, characterized by:
1. Different ranges (in the derived class and the base class, respectively);
2, the function name is the same;
3, the same parameters;
4. The base class function must have the virtual keyword;
Shadowing refers to a function of a derived class that masks a base class function with the same name.
1. If the function of the derived class has the same name as the function of the base class, but the parameters are different, at this time, whether or not the virtual off
Key, the function of the base class is hidden (note not to be confused with overloading).
2, if the function of the derived class has the same name as the function of the base class, and the parameters are the same, but the base class does not have virtual shut
Key word. At this point, the functions of the base class are hidden (note confusion with overrides).
Parameter default values can only appear in the declaration of a function and cannot appear in the definition body.
There are multiple parameter default parameters that can only be defaulted from backward forward.
constructors, destructors, and assignment functions are the most basic functions of each class.
Usage rules for constructor initialization:
1. If there is an inheritance relationship, the derived class must call the constructor of the base class in its initialization table.
2. A const constant of a class can only be initialized in the initialization table, because it cannot be initialized in the function's body in a copied manner.
3, the initialization of the data members of the class can take the initialization of the table or function in the body of two methods of assignment, the efficiency of the two methods is not exactly the same.
Member objects of non-intrinsic types should be initialized in the first way for higher efficiency.
The constructors, destructors, and assignment functions of a base class cannot be inherited by derived classes.
The constructor of the derived class should call the constructor of the base class in its initialization table.
The constructor for the base class and the derived class should be virtual (vritual).
When writing assignment functions for derived classes, be careful not to forget to re-assign values to the data members of the base class.
Use of the const modifier parameter:
Input parameters for non-intrinsic data types. "Value passing" should be changed to "const reference passing" in order to improve efficiency.
For input parameters of an internal data type, do not change the "value passing" method to "Const reference pass". Not only does not improve the efficiency, but also reduces the readability of the function.
The return value of the function is modified with the const:
The contents of the function return value (that is, the pointer) are not ENG modified if the return value of the function returned by the "pointer Pass" method is added to the const modifier. The return value can only be assigned to a const-decorated pointer of the same type.
If the return value of a function is in the form of "value passing", the const value is worthless because the function copies the return value into an external temporary storage unit.
There are not many occasions when the return value of a function is used as a "reference pass", which is usually only in the assignment function of the class, and is intended to be a chain expression.
Const member functions:
Any function that does not modify a data member should be declared as a const type. The Const keyword can only be placed at the tail end of a function declaration.
This article is from the "end-of-the-guest" blog, please be sure to keep this source http://zheng2048.blog.51cto.com/10612048/1790040
High-quality C + + reading notes (for review)