4.21 notes

xiaoxiao2021-03-05  38

Pinline

PINDEX? ?

The nature of "reference delivery" is like "pointer", and writing the way "value transfer". In fact, anything "pointer" that can be done can also do, why should I "quote"?

The answer is "Doing the right tool to work with appropriate tools."

The pointer can operate how things in memory without restraint, although the pointer is powerful, it is very dangerous. Just like a knife, it can be used to cut trees, paper cuts, manicure, haircut, etc., who dares to use it?

If you only need to borrow a "alias" of an object, then use "reference", not to use "pointer" to avoid accidents. For example, someone needs a certificate that I originally opened the official seal on the document. If you give him the key to him, then he gains the right.

In the C language, there are three ways to pass the parameters and return values: value transfer, pointer delivery, and reference delivery.

Quote:

n is equivalent to the alias (nickname), any operation to N is the operation of M. For example, some people named Wang Xiaoyao, his nickname is "three hair". Saying how "San Mao" is, in fact, it is three four four. So n is neither a copy of M, nor pointing to M's pointers, in fact, N is M itself.

Value delivery:

Passing a variable, in addition to the type definition, nothing, is the value transfer

Memory assignment:

There are three ways of memory allocation:

(1) Allocation from a static storage area. There is already a hidden when there is program compilation, and there is existence of the entire running period of the program. For example, global variables, static variables.

(2) Created on the stack. When performing a function, the storage unit of local variables within the function can be created on the stack, and the memory cells are automatically released at the end of the function. The stack memory allocation operation is within the command set of the processor, the efficiency is high, but the assigned memory capacity is limited.

(3) Distribution from the pile, also known as dynamic memory allocation. The program applies for any number of memory with Malloc or New when running, and the programmer will release the memory with free or delete. Dynamic memory survival is determined by us, it is very flexible, but the problem is also the most

CONST:

Seeing the const keyword, the C programmer first thinks of the constant constant. This is not a good condition reflection. If you only know the constant defined with const, it is equivalent to only use gunpowder to make firecrackers. Const greater charm is that it can modify the parameters, return values, or even function of the function.

Parameters of a const modified function

For input parameters for non-internal data types, "value delivery" should be changed to "const reference", and the purpose is to improve efficiency. For example, Void Func (a a) is changed to Void Func (Const A & A).

For input parameters for internal data types, do not change the "value to" to "Const reference". Otherwise, it will not only improve the efficiency, and the understandability of the function is reduced. For example, Void Func (INT X) should not be changed to Void Func (Const Int & X).

Return value with const modified functions

* If the value is added to the "pointer" mode to add const modification, the content of the function return value (ie, the pointer) cannot be modified, and the return value can only be assigned to the same type of CONST modified.

For example functions

Const char * getString (void);

The following statement will appear compilation:

CHAR * STR = getString ();

The correct usage is

Const char * str = getString (); * If the function return value uses "Value Transfer Method", since the function copies the return value to an external temporary storage unit, there is no value.

For example, don't write the function INT GetInt (Void) into const Int GetInet (VOID).

Do not write the function a geta (void) as a CONST A GETA (Void), which is a user-defined data type.

If the return value is not an internal data type, the function A geta (void) is indeed increasing efficiency. But it is necessary to be careful at this time, be sure to figure out the "copy" of the function wants to return an object or returns "alias", otherwise the program will be wrong. See Section 6.2 "Return Value Rules".

◆ Function return value is not much in the case of "reference delivery", which typically only appears in the assignment function of the class, and the purpose is to achieve chain expression.

Const member functions 1: Do not modify data members

2: Will not call members function without const modification

Any function that does not modify the data should be declared as a const type. If you write a Const member function, you accidentally modify the data member, or call other non-Const member functions, the compiler will point out errors, which will undoubtedly improve the robustness of the program.

In the following procedure, the member function getCount of class stack is only used to count, and the getCount should be a const function from logically. The compiler will point out errors in the getCount function.

Class Stack

{

PUBLIC:

Void Push; INT ELEM

INT POP (Void);

INT getCount (void) const; // constist member function

Private:

INT m_num;

INT M_DATA [100];

}

INT Stack :: getCount (void) Const

{

m_num; // Compiling errors, attempt to modify data members m_num

POP (); // Compiling errors, attempt to call non-const Functions seem to call Const modified member functions

Return m_num;

}

The statement of the Const member function looks whims: Const keyword can only be placed in the end of the function declaration, which is probably because it has been occupied elsewhere.

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

New Post(0)