Reference parameter, reference return value

Source: Internet
Author: User

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

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.