C++学习笔记01——引用、对象、和指针的区别

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
bool isShorter( string &s1, string s2,string *s3){
 s1="change s1";
 s2="change s2";
 (*s3)="change s3";
 return s1.size()<s2.size(); `
}
int main(int argc, char* argv[])
{
 string s1=" my name is s1";
 string s2=" my name is s2";
 string s3=" my name is s3";
 isShorter(s1,s2,&s3);

 cout<<s1 << endl;
 cout<<s2<<endl;
 cout<<s3<<endl;
 return 0;
}

out:change s1

        my name is s2

       change s3;

总结:C++中只有两种传递方式:值传递和引用传递。

        对象形参和指针形参都是值传递,形参要复制实参的值;引用形参是对象的别名,是对象本身。

原文地址:https://www.cnblogs.com/cgw0827/p/2793803.html