Simple way to improve transmission efficiency

zhaozj2021-02-17  54

Read "Thinking In C " feel:

When the parameter is passed in a pass-by-value, if the incoming parameters are modified, the efficiency can be improved in a constant reference (consTreference). Class cclass {...};

Void func1 (cclass a); // direct pass value

Void Func2 (Const Cclass & B); // Const Reference

When the FUNC1 is called, the stack is a CCLASS object that causes the call constructor (Copy-construct) to generate a temporary object inside the function. When the function returns, the object to release the object will cause the caller of the destructor.

For functions FUNC2, it is just an address that is pressed into the stack when calling, thereby saving overhead in FUNC1.

But use the reference call to have a shortcomings, that is, the function hides the program that it is the fact that it is transmitted. For example, there is a function as defined below: Void foo (CCLASS & A);

The call is as follows: CCLASS CA; Foo (CA); // may be mistaken at this time that the incoming value is only a value, if the value of the CA is changed, the error // debugging is also difficult

The solution is that the unified function is customized: the parameters to be modified within the function should be pointed; the parameters that are not modified are const reference method.

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

New Post(0)