C ++ language actual combat skills (1)

zhaozj2021-02-17  53

C language actual combat skills

1 presence

Learning a basic principle of programming language is a matter, and how to use this language design and design and design, high-quality procedures is another matter. C is a programming with an unusual functional and expression capability, as long as it is used, C can make the work fun. Whether it is object-oriented or conventional software design, C can directly represent and efficiently.

Design a set of classes (CLASS) by means of reasonable, careful ideas, they can automatically do memory management, alias, initialization, and clearing, type conversion, and solve some other programs A series of puzzles. However, C will generate some code, and they are wrong, and they cannot maintain, no scalability, and low efficiency. Even if you can know what you want to do, do you guarantee it accurately? For example, how should it run when the Operator New is not found? When is the destructor of the class be a virtual? How to write a member to initialize the table? If these problems are improperly handled, they often lead to some expectations, and very likely to be fatal program errors.

This article wants to tell you something will tell you how to write high quality C programs.

2 c to C transition

First of all, I want to discuss some of C and C from the advantages of C .

Since C is designed and implemented on the C language, it can be said that C is a subset of C , and all C language functions are valid, but there are many habits and programming styles.

2.1 Use const to replace the previous #define

The most important reason is that define is not part of the language itself. For example, you are defined

#define pi 3.1415926

This PI will never be seen by the compiler because it has been deleted by the pre-processing before the source code arrives before the compiler. If an error occurs when compiling, the reported error message is 3.1415926, not Pi.

Solving the above problems has a most convenient and profile approach:

CONST FLOAT PI = 3.1415926;

These constants are generally defined in the header file, and the point to explain is that the pointer itself is also important to indicate that the constant is also important in addition to the content points to the pointer.

Const Int * const pi = 3.1415926;

I will discuss the next discussion about Const.

There is also a very common situation. I believe that everyone will definitely have encountered many times. Here I will give a typical example:

#define max (a, b) ((a) (b)? (a): (b))

This is a simple macro problem, but it is!

Imagine the situation below, you will find how trouble:

INT A = 1, B = 0;

MAX (A , B);

MAX (A , B 10);

Max (A, "Hello");

Think carefully, you will find that A first value is added twice, and the second A is increased once, and the comparison of the third A is not conforming to grammar. You should find it here! Define will make your program how big! The most interesting thing is that the compiler will not ask any warnings for the previous two errors. When you are tracking the program error, how long will you spend?

Of course, C provides a very good way to solve the above problems, that is, INLINE. You can write the above macros into the inline function: Inline int Max (int A, int b) {RETURN A> B? A: B;}

Now it has been different from the macro above, and this very hidden mistake will not occur.

转载请注明原文地址:https://www.9cbs.com/read-28571.html

New Post(0)