The function parameter poem refers to the program output, such as the last comment of the program, indicating that the reference s represents the Object S2.
//a reference in a function#include <iostream>using namespacestd;classSample {intx; Public: Sample (inta): X (a) {cout<<"Call constructor Sample (int a)"<<Endl; } sample (Sample&a): X (a.x) {cout<<"Call Constructor sample (sample &a)"<<Endl; } intGetX () {returnx;}};//formal parameters are referencesvoidDisp (Sample &s) {cout <<s.getx ();}//void disp (Sample s) {cout << s.getx ();}intMain () {Sample S1 ( A), S2 (S1); DISP (S2); return 0;}/*Outputcall Constructor sample (int a) Call constructor sample (sample &a)*/
A function parameter is not a reference to a program output such as a comment, the result is a copy constructor that calls two class sample, the first time it is called when the S2 object is created, the second is called when the parameter is passed, and the copy constructor is called when the S1 object is copied to the formal parameter s.
#include <iostream>using namespacestd;classSample {intx; Public: Sample (inta): X (a) {cout<<"Call constructor Sample (int a)"<<Endl; } sample (Sample&a): X (a.x) {cout<<"Call Constructor sample (sample &a)"<<Endl; } intGetX () {returnx;}};//formal parameter is not a reference//void disp (Sample &s) {cout << s.getx ();}voidDisp (Sample s) {cout <<s.getx ();}intMain () {Sample S1 ( A), S2 (S1); Disp (S1); return 0;}/*Outputcall Constructor sample (int a) Call constructor sample (sample &a) call constructor sample (sample &a) 22 */
The return value is a non-reference object that returns the object in the stack, and the function returns the object in the stack after it disappears.
#include <iostream>using namespacestd;classSample {intx; Public: Sample () {} sample (inta): X (a) {cout<<"Call constructor Sample (int a)"<<Endl; } sample (Sample&a): X (a.x) {cout<<"Call Constructor sample (sample &a)"<<Endl; } ~Sample () {cout<<"Call destructor"<<Endl; } intGetX () {returnx;}};//formal parameters are referencevoidDisp (Sample &s) {cout<< S.getx () <<Endl;;}//The return value is not a referenceSample Copy (Sample &a) {Sample s (a.getx ()); cout<<"Call copy function"<<Endl; returns;}intMain () {Sample S1 ( A), S2; S2=copy (S1); DISP (S2); return 0;}/*Outputcall Constructor sample (int a) Call constructor sample (int. a) call copy Functioncall Destructor22call Destructorcall destructor*/
#include <iostream>using namespacestd;classSample {intx; Public: Sample () {} sample (inta): X (a) {cout<<"Call constructor Sample (int a)"<<Endl; } sample (Sample&a): X (a.x) {cout<<"Call Constructor sample (sample &a)"<<Endl; } ~Sample () {cout<<"Call destructor"<<Endl; } intGetX () {returnx;}};//formal parameters are referencevoidDisp (Sample &s) {cout<< S.getx () <<Endl;;}//The return value is a referencesample& Copy (Sample &a) {//local variable, program issue warning, reference local variableSample S (a.getx ()); cout<<"Call copy function"<<Endl; returns;}intMain () {Sample S1 ( A), S2 =copy (S1); //s2 = copy (S1);disp (S2); return 0;}/*Outputcall Constructor sample (int a) Call constructor sample (int. a) call copy Functioncall Destructorcall constructor Sample (sample &a) 2686744call Destructorcall destructor*/intMain () {Sample S1 ( A), S2; S2=copy (S1); DISP (S2); return 0;}/*Outputcall Constructor sample (int a) Call constructor sample (int. a) call copy Functioncall Destructor22call Destructorcall destructor*/
Reference parameter, reference return value