[CPP]View Plaincopy
- //============================================================================
- Name:CppLab.cpp
- Author:sodino
- Version:
- Copyright:your Copyright Notice
- Description:hello World in C + +, Ansi-style
- //============================================================================
- #include <iostream>
- #include <string>
- Using namespace std;
- struct student{
- String name;
- };
- int main () {
- void print (Student);
- void Print_point (Student *);
- void Print_reference (Student &);
- struct Student stu = {"Yao Ming"};
- cout << "main &stu=" << &stu << endl << Endl;
- Print (STU);
- cout << "after print () name=" << stu.name << "no changed." << Endl << Endl;
- Print_point (&stu);
- cout << "after Print_point () name=" << stu.name << "have been modified." << Endl << Endl
- Print_reference (Stu);
- cout << "after Print_reference () name=" << stu.name << "have been modified." << Endl;
- return 0;
- }
- void print (Student stu) {
- //argument to parameter, it consumes extra time. Print_reference () is much more efficient.
- cout << "Print () Stu address=" << &stu << "is different." << Endl; //Parameter Stu and the Stu outside of the function body are two different objects!!
- Stu.name = "New.name"; //The assignment here does not change the name of the function outside the Stu
- cout << "Print () set new Name=" << stu.name << Endl;
- }
- void Print_point (Student * stu) {
- Stu->name = "New.point.name";
- cout << print_point () set new name= << stu->name << Endl;
- }
- void Print_reference (Student &stu) {
- Stu.name = "New.reference.name";
- cout << "set new Name=" << stu.name << Endl;
- }
[HTML]View Plaincopy
- Main &stu=0x7fff5eabfbc8
- Print () Stu address=0x7fff5eabfba0 is different.
- Print () set new name=new.name
- After print () name=Yao Ming no changed.
- Print_point () set new name=new.point.name
- After Print_point () name=new.point.name have been modified.
- Set new name=new.reference.name
- 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