The difference between [C + +] variables, pointers, and references as function parameters

Source: Internet
Author: User
Tags ming

[CPP]View Plaincopy
  1. //============================================================================
  2. Name:CppLab.cpp
  3. Author:sodino
  4. Version:
  5. Copyright:your Copyright Notice
  6. Description:hello World in C + +, Ansi-style
  7. //============================================================================
  8. #include <iostream>
  9. #include <string>
  10. Using namespace std;
  11. struct student{
  12. String name;
  13. };
  14. int main () {
  15. void print (Student);
  16. void Print_point (Student *);
  17. void Print_reference (Student &);
  18. struct Student stu = {"Yao Ming"};
  19. cout << "main &stu=" << &stu << endl << Endl;
  20. Print (STU);
  21. cout << "after print () name=" << stu.name << "no changed."  << Endl << Endl;
  22. Print_point (&stu);
  23. cout << "after Print_point () name=" << stu.name << "have been modified." << Endl <<  Endl
  24. Print_reference (Stu);
  25. cout << "after Print_reference () name=" << stu.name << "have been modified." << Endl;
  26. return 0;
  27. }
  28. void print (Student stu) {
  29. //argument to parameter, it consumes extra time.  Print_reference () is much more efficient.
  30. cout << "Print () Stu address=" << &stu << "is different." << Endl;  //Parameter Stu and the Stu outside of the function body are two different objects!!
  31. Stu.name = "New.name"; //The assignment here does not change the name of the function outside the Stu
  32. cout << "Print () set new Name=" << stu.name << Endl;
  33. }
  34. void Print_point (Student * stu) {
  35. Stu->name = "New.point.name";
  36. cout << print_point () set new name= << stu->name << Endl;
  37. }
  38. void Print_reference (Student &stu) {
  39. Stu.name = "New.reference.name";
  40. cout << "set new Name=" << stu.name << Endl;
  41. }
[HTML]View Plaincopy
  1. Main &stu=0x7fff5eabfbc8
  2. Print () Stu address=0x7fff5eabfba0 is different.
  3. Print () set new name=new.name
  4. After print () name=Yao Ming no changed.
  5. Print_point () set new name=new.point.name
  6. After Print_point () name=new.point.name have been modified.
  7. Set new name=new.reference.name
  8. After Print_reference () name=new.reference.name have been modified.

Print (): Using struct variables as arguments and formal parameters is straightforward, but when you call a function additional memory is needed for formal parameters, the entire contents of the argument are passed through a value of one by one to the parameter. resulting in wasted space and time. Print_point (): Specifies also as an argument and a parameter, the argument simply passes the starting address of the Stu to the formal parameter, not the one by one pass, and there is no additional memory opening and high efficiency. But readability may not be good. Print_reference (): The argument is a struct student type variable, and the formal parameter is referenced by that type, and during the execution of the function, the Stu of the function body operation is Stu outside the function body, and the readability is also strong. Add reference variables in C + + to improve efficiency while maintaining high readability.

[C + +] variable, pointer, reference as function parameter difference

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.