首页 > 编程语言 > 详细

c++ 中在形参与实参之间的值传递

时间:2014-11-17 17:33:13      阅读:247      评论:0      收藏:0      [点我收藏+]

主要是对比直接传递与引用类型、指针类型之间的区别。

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class MyClass
 5 {
 6 public:
 7     int a;
 8     void method();
 9 };
10 void MyClass::method()
11 {
12     cout<<"the last value of class:a after fun:"<<a<<\n;
13 }
14 
15     void fun1(MyClass);
16     void fun2(MyClass*);
17     void fun3(MyClass&);
18 int main()
19 {
20     MyClass myclass;
21     myclass.a=10;
22     fun1(myclass);
23     cout<<" the initial value of class:a after fun1:  "<<myclass.a<<\n;
24     myclass.a=10;
25     fun2(&myclass);
26     cout<<" the initial value of class:a after fun2:  "<<myclass.a<<\n;
27     myclass.a=10;
28     fun3(myclass);
29     cout<<" the initial value of class:a after fun3:  "<<myclass.a<<\n;
30     system("pause");
31 }
32 void fun1(MyClass mc)
33 {
34     mc.a=40;
35     mc.method();
36 }
37 void fun2(MyClass* mc)
38 {
39     mc->a=60;
40     mc->method();
41 }
42 void fun3(MyClass&mc)
43 {
44     mc.a=80;
45     mc.method();
46 }

参照:http://www.cnblogs.com/wackelbh/archive/2009/12/29/1984064.html

c++ 中在形参与实参之间的值传递

原文:http://www.cnblogs.com/xiguapi/p/4103825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!