Pointer passing during function call

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.