C + + language is an object-oriented language, the use of C + + written code more simple, efficient, more maintainable and reusable. But many people use the C + + language but feel C + + and C programming is no different. This is actually due to the C + + language characteristics and characteristics of understanding and use of not enough. In fact, no programmer can use the C language programming efficiency to exceed the C + + language.
Use new and delete for dynamic memory allocation and release
Operators new and delete are new C + + operators that provide the dynamic allocation and deallocation of storage. It acts as a function of the C language malloc () and free (), but the performance is superior. There are several advantages to using new compared to using malloc ():
(1) New automatically calculates the size of the type to be allocated, without using the sizeof operator, which makes it easier to avoid errors.
(2) automatically returns the correct pointer type without forcing pointer type conversions.
(3) The assigned object can be initialized with new.
Use examples:
(1)int p;
p=new int[10]; //分配一个含有10个整数的整形数组
delete[] p; //删除这个数组
(2)int p;
p=new int (100);//动态分配一个整数并初始化
Second, use the inline function to replace the macro call
For frequently used functions, the C language recommends using a macro call instead of a function call to speed up code execution and reduce the call overhead. However, there are many drawbacks of macro invocation, which may cause undesirable side effects. For example macros: #define ABS (a) ((a) <0? (a): (a)), the macro will go wrong when using ABS (i++).
Therefore, in C + + should use the inline function to replace the macro call, so that the purpose of the macro call can be achieved, and avoid the drawbacks of the macro call.
Use the inline function to simply place the inline keyword in front of the function return type. For example:
inline int Add(int a,int b);//声明Add()为内连函数
This way, when the compiler encounters the Add () function, it no longer makes a function call, but embeds the function code directly to speed up the execution of the program.
Third, use function overload
In the C language, the names of two functions cannot be the same or cause compilation errors. In C + +, two functions with the same function name and different parameters are interpreted as overloads. For example:
void PutHzchar str //在当前位置输出汉字
void PutHzint xint ychar str //在x,y处输出汉字
Using function overloads can help programmers deal with more complex problems, avoid the use of miscellaneous function names such as Intabs (), Fabs (), Dabs (), and, in large programs, make function names easy to manage and use without having to rack up the function names.
Using reference (reference) instead of pointers for parameter passing
In C, when a function needs to modify the value of a variable used as a parameter, the argument should be declared as a pointer type. For example:
void Addint a a++
However, for complex programs, the use of pointers is error-prone, and the program is difficult to read. In C + +, references can be used instead of pointers to make the program clearer and easier to understand. A reference is an alias to a variable, and the reference is manipulated, which is equivalent to manipulating the original variable. For example, a function that uses a reference is defined as:
void Addint a a++ //a为一个整数的引用
This function is the same as the function that uses the previous function of the pointer, but the code is simpler and more readable.