注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

随机过程

http://superware.blog.163.com

 
 
 

日志

 
 

变量、指针、引用与形参初始化  

2014-07-07 19:56:39|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

        C++ 关于函数参数是这样规定的:按照形参初始化方式,函数参数有两种:拷贝初始化方式和直接初始化方式。

拷贝初始化:形参初始化是通过拷贝实参来实现;
直接初始化:形参初始化是通过直接赋予变量地址的方式来实现。


        有个认识:对变量的访问都是通过地址访问的,在汇编语言中,变量存储与内存中,变量占据内存块,并有一个首地址。对变量的访问必须首先确定首地址。首地址的确定方式有两种:直接访问和间接访问。直接访问是通过变量地址访问,间接访问是通过内存中所存储的另一个变量的地址进行间接访问。

一、拷贝初始化形参

        拷贝初始化形参有两种:直接形参和指针形参。

1、直接形参如下:

void swap_cpy (int a, int b)

{

...

}

直接形参是将实参的值拷贝给形参,对形参的访问与实参没任何关系。

2、指针形参如下:

void swap_ptr (int *a, int *b)

{

...

}

指针形参方式是:将实参的地址拷贝给指针变量,而指针的地址才是直接访问指针变量的地址。

二、非拷贝初始化形参


        直接初始化形参方式就是引用

void swap_ref (int &a, int &b)

{

...

}

引用就是将实参的地址直接给形参,形参地址就是实参地址。

三、举例说明

#include <iostream>

using namespace std;

void swap_cpy (int a, int b)
{
int c;

c = a;
a = b;
b = c;

cout << "--- swap_cpy (int a, int b) begins ---" << endl;
cout << "a = " << a << ", " << "b = " << b << endl;
cout << "&a = " << &a << ", " << "&b = " << &b << endl;
cout << "--- swap_cpy (int a, int b) ends --- " << endl;
}

void swap_ptr (int *a, int *b)
{
int c;

c = *a;
*a = *b;
*b = c;

cout << "--- swap_cpy (int *a, int *b) begins ---" << endl;
cout << "*a = " << *a << ", " << "*b = " << *b << endl;
cout << "a = " << a << ", " << "b = " << b << endl;
cout << "&a = " << &a << ", " << "&b = " << &b << endl;
cout << "--- swap_cpy (int *a, int *b) ends ---" << endl;
}

void swap_ref (int &a, int &b)
{
int c;

c = a;
a = b;
b = c;

cout << "--- swap_cpy (int &a, int &b) begins ---" << endl;
cout << "a = " << a << ", " << "b = " << b << endl;
cout << "&a = " << &a << ", " << "&b = " << &b << endl;
cout << "--- swap_cpy (int &a, int &b) ends ---" << endl;
}

void try_ref ()
{
int x = 1;
int y = 8;

cout << "x = " << x << ", " << "y = " << y << endl;
cout << "&x = " << &x << ", " << "&y = " << &y << endl;
cout << endl;

cout << "*** before swap_cpy (int x, int y) *** " << endl;
cout << "x = " << x << ", " << "y = " << y << endl;
swap_cpy (x, y);
cout << "*** after swap_cpy (int x, int y) ***" << endl;
cout << "x = " << x << ", " << "y = " << y << endl;
cout << endl;

cout << "*** before swap_ptr (int *x, int *y) ***" << endl;
cout << "x = " << x << ", " << "y = " << y << endl;
swap_ptr (&x, &y);
cout << "*** after swap_cpy (int *x, int *y) ***" << endl;
cout << "x = " << x << ", " << "y = " << y << endl;
cout << endl;

cout << "*** before swap_ptr (int &x, int &y) ***" << endl;
cout << "x = " << x << ", " << "y = " << y << endl;
swap_ref (x, y);
cout << "*** after swap_cpy (int &x, int &y) ***" << endl;
cout << "x = " << x << ", " << "y = " << y << endl;
cout << endl;
}

int main ()
{
try_ref ();

return 0;
}

结果:

x = 1, y = 8
&x = 0x7fffee33dfac, &y = 0x7fffee33dfa8 // 实参地址

*** before swap_cpy (int x, int y) ***
x = 1, y = 8
--- swap_cpy (int a, int b) begins ---
a = 8, b = 1
&a = 0x7fffee33defc, &b = 0x7fffee33def8 // 形参地址,不同于实参地址
--- swap_cpy (int a, int b) ends ---
*** after swap_cpy (int x, int y) ***
x = 1, y = 8

*** before swap_ptr (int *x, int *y) ***
x = 1, y = 8
--- swap_cpy (int *a, int *b) begins ---
*a = 8, *b = 1
a = 0x7fffee33dfac, b = 0x7fffee33dfa8 // 指针变量的值,是实参地址
&a = 0x7fffee33def8, &b = 0x7fffee33def0 // 指针变量地址
--- swap_cpy (int *a, int *b) ends ---
*** after swap_cpy (int *x, int *y) ***
x = 8, y = 1

*** before swap_ptr (int &x, int &y) ***
x = 8, y = 1
--- swap_cpy (int &a, int &b) begins ---
a = 1, b = 8
&a = 0x7fffee33dfac, &b = 0x7fffee33dfa8 // 引用就是实参地址
--- swap_cpy (int &a, int &b) ends ---
*** after swap_cpy (int &x, int &y) ***
x = 1, y = 8

  评论这张
 
阅读(51)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017