码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-17 17:34:55      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   div   

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

 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++ 中在形参与实参之间的值传递

标签:style   blog   http   io   color   ar   os   sp   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!