The problem about function parameter modification of Huawei Machine test summary

Source: Internet
Author: User

The first second question is relatively simple, the third problem is good, is a recursive, but the time of the transfer of a problem, I made a flag to mark, but the function changed flag, but after the exit function, flag does not change, think up, it should be no incoming pointer, therefore, to learn.

http://blog.csdn.net/herecles/article/details/6072523

In C + +, if the type of the argument of a function is a data type with a large data type, this is a great inconvenience if you use a generic parameter, which is helpful if you can pass an address or a reference to the original parameter to improve performance.

This paper studies the differences and relations between reference and pointer parameters. In fact, reference and pointer parameters can be used to modify the source data within the bar function, which is common, but they are also different. Here is an example:

[CPP]View Plain Copy
  1. #include <iostream>
  2. using namespace Std;
  3. void Swapbypoint (int* x,int* y); Declaration of Pointer Pass function
  4. void swapbyreference (int &x,int &y); Declaration of a reference parameter function
  5. int main ()
  6. {
  7. int x = 3;
  8. int y = 5;
  9. cout<< "before conversion: x=" <<x<< "y=" <<y<< "/n";
  10. Swapbypoint (&x,&y); If Swapbypoint (x, y) is used here, the error is: cannot convert parameter 1 from ' int ' to ' int * '
  11. cout<< "pointer transfer after conversion: x=" <<x<< "y=" <<y<< "/n";
  12. Swapbyreference (x, y); If Swapbyreference (&x,&y) is used here, the error cannot convert parameter 1 from ' int * ' to ' int & '
  13. cout<< "referring to a parameter call after conversion: x=" <<x<< "y=" <<y<< "/n";
  14. System ("pause");
  15. return 0;
  16. }
  17. /* Swap function use pointer to pass the parameter */
  18. void Swapbypoint (int *x,int *y)
  19. {
  20. int temp = *x;
  21. *x = *y;
  22. *y = temp;
  23. }
  24. /*
  25. Use reference to pass a parameter
  26. */
  27. void swapbyreference (int &x,int &y)
  28. {
  29. int temp = x;
  30. x = y;
  31. y = temp;
  32. }

The effect of pointer and reference parameters is the same.

Their differences personally believe that:

    • The pointer parameter passes the address of an argument (here is an int argument) so that, although the arguments are different from the formal parameters, they only want the same address, so the operation of the number of the same address affects the original number.
    • The reference pass is a parameter itself, but in the calling function, the value of the address that holds them is exchanged.

The invocation of the two methods must be the above, otherwise it will error, in the corresponding code is prompted .....

The problem about function parameter modification of Huawei Machine test summary

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.