Rereading Essential C + + study notes 2
Rereading Essential C + + study notes 2
By sssa2000
Chapter 2: Process-oriented programming style
I am also very long ago puzzled why a process-oriented style of object-oriented programming, although there are now very deep experience. In fact, no matter what style, as long as it can better address the issue is good style.
1, Chuan Chuan and the value of the site:
Lippman in explaining this problem by the time an exploration process, the obstacles that beginners are not leading into this problem.
What is the shape parameter? What is it is the Senate? Put simply, the preparation of the function of the parameters that shape the Senate and the calling function of the parameters when it is the Senate.
When you call a function, in memory of the establishment of a special regional, called stack. He did not provide a function parameter storage space.
In default, the parameter will be copied into a stack, which is the so-called value-passing, and have set up to sort an array, the use-value approach is not going to change the original series, which is used to Chuan site. Parameters in the front plus a "&" button.
When the used-site? When the hope that the introduction of the revised target, or if imported parameter object is too large, used-site will be greatly improved the effectiveness of the procedure.
Of course, can be used to guide transmission parameters, the fact is the same, because the nature of guidelines that address.
2, small gleaned dynamic memory management
We know that the new method can be used statement dynamic memory space, I do not know very easily from a literal understanding of his message. Statement by this method are not local variables are not the overall situation, it is established in the heap, it can be destroyed.
For example, int a [5] = new int [5] When used to be destroyed when a delete [] he would destroy all of the array elements.
Of course will cause improper use of Memory Leak.
3, the default parameters:
Use the default parameters can be more flexible so that we call functions, the default parameters of a few rules:
First of all, if we provide a default parameters, then the right of the parameters should all parameters parameter is the default. Therefore, we should default parameters on the right.
The default parameters specified only once in the function declaration places can be defined, but not the two places designated. To enhance visibility, the authors suggest that we place on the statement.
4, on inline function
Why should inline function? However, a function to be very frequently invoked, the compiler when the burden will be very heavy, we could take him for the statement inline function to allow the compiler to begin to reduce the burden. Method: In the original inline functions can be added to the front. Inline function definition is often the first place in the document.
5, the use of templates:
Key words Functoin template to template opening, for example:
Template <typename elemType> displaly (const string & msg, const vector <elemType> & vec)
(
………
)
The use of the time is also very simple:
Vector <string> ivec; / / here can be any type
String msg;
Display (msg, ivec);
6, function indicators:
Function pointer to bring us greater flexibility. We can use different indicators to choose the function to call.
Function pointer general and with the use of an array:
For example, there are so few function:
* Const vector <int> fibon_seq (int size);
* Const vector <int> lucas_seq (int size);
* Const vector <int> pell_seq (int size);
* Const vector <int> pent_seq (int size);
We intend to use a function pointer to call a flexible, do the following:
First declare a function pointer: const vector <int> * (* seq_ptr) (int);
Then define an array: const vector <int> * (* seq_array []) (int) = (fibon_seq, lucas_seq, pell_seq, pent_seq);
This is a pointer to an array of storage function, so that we can set up an index value: int seq_index
Then we can seq_array seq_ptr = [+ + seq_index]; to control the function call.
7, the first document on the maintenance of:
Statement on the function of the first document is a good way to proceed, when a different procedure calls this function when it is not necessary for each statement. But not in the best definition of header files inside.
Chapter II End








0 Comments to “Rereading Essential C + + study notes 2”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.