Pointers are powerful tools for C/C ++, but they are also the most error-prone.
C ++ supports three methods for function calling: Value assignment transfer, reference transfer, and pointer transfer.
1. The value assignment is to create a temporary object in the function call stack and copy the real parameter object to the temporary object. The function can only operate on the copy of the real parameter object. This method does not matter for the basic type, but for a large class object, it will bring a great call price.
2. the alias of the Real-parameter object is used to reference the real-parameter object. instead of copying the real-parameter object, the function can directly operate the real-parameter object. Therefore, it is efficient and suitable for transferring large objects, at the same time, this method can also be used to return data from the function. Its disadvantage is that the real parameter object must exist before the function is called.
3. pointer passing is essentially a value assignment, and the pointer variable size is fixed. Therefore, it is suitable for passing large objects. Of course, it can also be used to return data from functions. At the same time, beginners may make mistakes. For example, they want to use the following function to obtain object pointers:
1 VoidGetmethod (Method *P_method)2 {3P_method =NewMethod ();4}
But the function only operates the copy of the real parameter object. The real parameter object we really want to change is not modified, so the real method is:
1VoidGetmethod (method **Pp_method)2 {3* Pp_method =NewMethod ();4}
Pointer passing during function call