Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48327
  • 博文数量: 12
  • 博客积分: 291
  • 博客等级: 二等列兵
  • 技术积分: 147
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 14:25
文章分类

全部博文(12)

文章存档

2011年(12)

我的朋友

分类: C/C++

2011-09-01 20:39:26

默认参数是存在于函数的声明中,还是函数的定义中呢?
    我在VS6.0和VS2008下做了如下实验,并做出了简单的总结,有不足或者不准确的地方,欢迎大家拍砖,我会及时修正相关内容。

    实验一:默认参数不能同时存在于函数声明和函数定义中

  1. #include <iostream>
  2. #include <tchar.h>
  3. using namespace std;

  4. void SetHeight(double dHeight = 183.5);

  5. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  6. {
  7.     SetHeight(181.5);
  8.     return 1;
  9. }

  10. void SetHeight(double dHeight = 183.5)
  11. {
  12.     cout << _T("身高为:") << dHeight << endl;
  13. }
然后调整一下顺序(本文的例子只用于实验,并不侧重于代码本身是否有意义):

  1. #include <iostream>
  2. #include <tchar.h>
  3. using namespace std;

  4. void SetHeight(double dHeight = 183.5);
  5. void SetHeight(double dHeight = 183.5)
  6. {
  7.     cout << _T("身高为:") << dHeight << endl;
  8. }
  9. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  10. {
  11.     SetHeight(181.5);
  12.     return 1;
  13. }
两处代码编译的时候,编译器都会报告如下错误:
error C2572: 'SetHeight' : redefinition of default parameter : parameter 1,see declaration of 'SetHeight'

实验一小结:编译器不允许默认参数同时存在于声明和定义中,之所以这个做,个人猜测:
1、没必要,声明和定义都有默认参数相比于声明或者定义中一处有默认参数,没什么多余的意义。
2、容易犯错,就上例而言,假如在声明中设置默认参数值为183.5,在定义中设置默认参数值为167.5,将导致错误的产生,所以,只允许其中一处设置默认参数可以避免这种形式的错误。

    实验二:默认参数的所在的位置需要在调用者的前面

  1. #include <iostream>
  2. #include <tchar.h>
  3. using namespace std;

  4. void SetHeight(double dHeight = 183.5);

  5. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  6. {
  7.     SetHeight();
  8.     return 1;
  9. }

  10. void SetHeight(double dHeight)
  11. {
  12.     cout << _T("身高为:") << dHeight << endl;
  13. }
上述代码编译通过。

  1. #include <iostream>
  2. #include <tchar.h>
  3. using namespace std;

  4. void SetHeight(double dHeight);

  5. void SetHeight(double dHeight = 183.5)
  6. {
  7.     cout << _T("身高为:") << dHeight << endl;
  8. }

  9. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  10. {
  11.     SetHeight();
  12.     return 1;
  13. }
上述代码编译通过。

  1. #include <iostream>
  2. #include <tchar.h>
  3. using namespace std;

  4. void SetHeight(double dHeight = 183.5);

  5. void SetHeight(double dHeight)
  6. {
  7.     cout << _T("身高为:") << dHeight << endl;
  8. }

  9. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  10. {
  11.     SetHeight();
  12.     return 1;
  13. }
上述代码编译通过。

  1. #include <iostream>
  2. #include <tchar.h>
  3. using namespace std;

  4. void SetHeight(double dHeight);

  5. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  6. {
  7.     SetHeight();
  8.     return 1;
  9. }

  10. void SetHeight(double dHeight = 183.5)
  11. {
  12.     cout << _T("身高为:") << dHeight << endl;
  13. }
上述代码在编译的时候,编译器报告如下错误:
error C2660: 'SetHeight' : function does not take 0 parameters

实验二小结:编译通过的代码都有一个共同点,无论默认参数在函数声明语句中还是在函数定义语句中,默认参数的位置都在调用该函数的语句之前(代码的编写顺序),而编译失败的代码是因为调用函数的语句在默认参数所在的语句之后,由此得出一个结论:默认参数的语句所在的位置需要在主调函数的语句的前面。

    实验三:函数的声明和定义分别位于不同文件中的默认参数规则

  1. // Head.h
  2. #pragma once
  3. #include <tchar.h>
  4. #include <iostream>
  5. using namespace std;

  6. void SetHeight(double dHeight = 183.5);

  1. //Body.cpp
  2. #include "Head.h"
  1. void SetHeight(double dHeight)
  2. {
  3.     cout << _T("身高为:") << dHeight << endl;
  4. }

  1. //Main.cpp
  2. #include "Head.h"

  3. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  4. {
  5.     SetHeight();
  6.     return 1;
  7. }
上述代码编译通过,因为Main.cpp中包含了Head.h,等价于将Head.h中的内容拷贝到#include"Head.h"的位置,所以在_main函数中的SetHeight()语句执行之前,就找到了带默认参数的函数声明。

  1. // Head.h
  2. #pragma once
  3. #include <tchar.h>
  4. #include <iostream>
  5. using namespace std;

  6. void SetHeight(double dHeight);
  1. //Body.cpp
  2. #include "Head.h"

  3. void SetHeight(double dHeight = 183.5)
  4. {
  5.     cout << _T("身高为:") << dHeight << endl;
  6. }
  1. //Main.cpp
  2. #include "Head.h"

  3. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  4. {
  5.     SetHeight();
  6.     return 1;
  7. }
上述代码在编译的时候,编译器报告如下错误:
error C2660: 'SetHeight' : function does not take 0 parameters
由此,我猜测:编译器在编译到_tmain函数的SetHeight()语句的时候,只找到了SetHeight的声明,并以此判断出函数参数有错误。所以,SetHeight函数的定义部分并未出现在主调函数语句之前。

  1. // Head.h
  2. #pragma once
  3. #include <tchar.h>
  4. #include <iostream>
  5. using namespace std;

  6. void SetHeight(double dHeight);
  1. //Body.cpp
  2. #include "Head.h"

  3. void SetHeight(double dHeight = 183.5)
  4. {
  5.     cout << _T("身高为:") << dHeight << endl;
  6. }

  7. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  8. {
  9.     SetHeight();
  10.     return 1;
  11. }
上述代码编译通过。

实验三小结:函数的声明和定义位于不同文件中的默认参数的规则同样要遵循实验二的结论。

上述的三个实验得出的两条结论:
1、编译器不允许默认参数同时存在于声明和定义中。
2、默认参数的语句所在的位置需要在主调函数的语句的前面。
同样适用于类的普通成员函数。
阅读(6743) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~