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
#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
相关文章推荐
- more effective c++之Item M1:指针与引用的区别
- C++ 中指针和引用的什么区别
- C++中引用传递与指针传递区别
- C、C++中引用与指针的区别
- c++中指针,指针的引用,指针的指针的区别
- c++指针与引用的区别
- C++ 中指针和引用的什么区别
- C++中引用与指针的区别与联系
- C++中指针(pointer)和引用(reference)的区别和联系
- java, C++ 指针和引用区别
- 浅谈C++中指针和引用的区别
- C++:指针传递、引用传递的区别
- c++引用与指针的区别(着重理解)
- C++中指针和引用的区别
- C++ 指针和引用的区别
- 浅谈C++中指针和引用的区别
- 浅谈C++中指针和引用的区别
- C++中引用和指针的区别
- C++指针与引用的区别
- C++ 类对象和 指针的区别