Reread Essential C ++ Reading Note 2

zhaozj2021-02-16  91

Reread Essential C Reading Note 2

By SSSA2000

7/25/2004

Chapter 2: Process-oriented programming style

I have been very confused for a long time, why have a process-oriented object-oriented programming style, although there is already a deep experience. In fact, no matter what style, as long as it can better solve the problem is a good style.

1, pass value and address:

Lippman uses an exploring process while explaining this problem, so that beginners have no obstacles to be led by this problem.

What is a ginseng? What is the argument? Simply put, the parameters illustrated when writing a function is a form, and the parameters when the function is called.

When a function is called, a special area is created in memory, and the program stack is called. He offers a storage space without a function parameter.

By default, the parameters will be copied in the incoming program, which is the so-called pass value, set to an array sort, and the way to use the value will not change the original number, this is to use it. site. Add a "&" in front of the parameter.

When should I use to address? When you want to modify the incoming object, or if the incoming parameter object is too large, it will greatly improve the efficiency of the program.

Of course, you can also use pointers to deliver parameters, which is also the same, because the nature of the pointer is the address.

2, small peek dynamic memory management

We know that using the New method can declare the dynamic memory space, I don't know how to understand what he mean from the literal. The variables declared with this method are not partially unsatisfactory, which is built on HEAP and can be destroyed at any time.

For example, Int a [5] = new int [5]; use delete [] a when you want to destroy.

Of course, improper use can cause Memory Leak.

3, default parameters:

Use the default parameters to make our more flexible call functions, the default parameters have several rules:

First, if we provide a default parameter, all parameters on the right of this parameter should be the default parameter. So we have to put the default parameters on the right side.

The default parameter can only be specified once, where the function declaration can be defined again, but cannot be specified in two places. In order to improve visibility, the author suggests that we are placed on the statement.

4, about inline function

Why is the inline function? But when a function is called very frequently, this time the compiler will be very heavy, we can declare him to inline to make the compiler to expand the function to reduce the burden. Method: Adding inline before the original function. The definition of the inline function is often placed in the header file.

5, use templates:

Functoin Template starts with keyword Template, for example:

Template Displading (Const String & MSG, Const Vector & VEC)

{

.........

}

It is also very simple when used:

Vector IVEC; / / This can be any type

String msg;

Display (MSG, IVEC);

6, function pointer:

Function pointers give us greater flexibility. We can use pointers to select different functions to call.

Function pointer is generally used with an array:

For example, there are such functions:

Const vector * fibon_seq (INT size);

Const vector * lucas_seq;

Const vector * Pell_Seq (INT size);

Const vector * pent_seq; we plan to use a function pointer to flexibly call, you can:

First declare a function pointer: const vector * (* SEQ_PTR) (int);

Then define an array: const vector * (* seq_array []) (int) = {Fibon_Seq, Lucas_seq, Pell_Seq, Pent_Seq};

This is an array of storage function pointers so that we can set an index value: int SEQ_Index

Then we can control call functions by seq_ptr = seq_array [ seq_index];

7. About maintaining the header file:

Putting the statement of the function in the header file is a good way, so that when different programs call this function, you can declare each time. But the definition is preferably not put it in the header file.

The second chapter is over

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

New Post(0)