您的位置:首页 > 编程语言 > C语言/C++

C++引用、对象、和指针的区别

2012-12-04 18:36 417 查看
#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++中只有两种传递方式:值传递和引用传递。 对象形参和指针形参都是值传递,形参要复制实参的值;引用形参是对象的别名,是对象本身。本文出自 “游子陈” 博客,请务必保留此出处http://cgw0827.blog.51cto.com/1850189/1078542
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: