C ++ programming skills in Programming Software Series

Source: Internet
Author: User
C ++ programming skills in the Programming Software Series-general Linux technology-Linux programming and kernel information. The following is a detailed description. C ++ is an object-oriented language. The code written in C ++ is simpler, more efficient, and more maintainability and reusable. However, many people use the C ++ language but feel that C ++ is no different from C programming. This is actually caused by insufficient understanding and use of the features and features of the C ++ language. In fact, no programmer can use C language to program more efficiently than C ++.

1. Use new and delete for dynamic memory allocation and release

The new and delete operators are new operators in C ++. They provide dynamic storage allocation and release functions. It is equivalent to the C language functions malloc () and free (), but the performance is superior. Using new has the following advantages over using malloc:

(1) new automatically calculates the size of the type to be allocated. It does not use the sizeof operator, which is easy to use and can avoid errors.

(2) the correct pointer type is automatically returned, and no forced pointer type conversion is required.

(3) You can use new to initialize the allocated object.

Example:

(1) int? P;
P = new int [10]; // assign an integer array containing 10 Integers
Delete [] p; // delete this array

(2) int? P;
P = new int (100); // dynamically allocates an integer and initializes it.

2. Replacing macro calls with inline functions

For frequently-used functions, we recommend that you use macro calls in C language instead of function calls to accelerate code execution and reduce call overhead. However, macro calls have many drawbacks and may cause undesirable side effects. For example, MACRO: # define abs (a) <0? (-A) :( a) when abs (I ++) is used, this macro will fail.

Therefore, the inline function should be used in C ++ to replace macro call. This can achieve macro call and avoid the disadvantages of macro call.

The inline keyword must be placed before the return type of the function to use the inlian function. For example:

Inline int Add (int a, int B); // declare Add () as the inner function

In this way, when the compiler encounters an Add () function, it will not call the function, but directly embed the function code to speed up program execution.

Iii. Use Function Overloading

In C, the names of the two functions cannot be the same; otherwise, compilation errors may occur. In C ++, two functions with the same function name and different parameters are interpreted as overload. For example:

Void PutHz? Char? Str? // Output Chinese characters at the current position
Void PutHz? Int x? Int y? Char? Str? // Output Chinese characters at x and y

Function overloading can help programmers deal with more complex problems, avoiding complicated function names such as intabs (), fabs (), dabs (), and so on, makes the function name easy to manage and use, without the need to handle the function name.

4. Use reference instead of pointer for parameter passing

In C, if a function needs to modify the variable value used as a parameter, the parameter should be declared as a pointer type. For example:

Void Add? Int? A ??? A ++ ??

However, for complex programs, the use of pointers is prone to errors, and the program is hard to understand. In C ++, you can use references to replace pointers in the preceding cases to make the program clearer and easier to understand. A reference is an alias for a variable, which is equivalent to an operation on the original variable. For example, the referenced function is defined:

Void Add? Int? A ++? ? // A is an integer reference

This function has the same functionality as the previous function using pointers, but the code is more concise and easy to understand.

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.