zoukankan      html  css  js  c++  java
  • 函数声明和函数定义中的默认参数浅析

    默认参数是存在于函数的声明中,还是函数的定义中呢?
        我在VS6.0和VS2008下做了如下实验,并做出了简单的总结,有不足或者不准确的地方,欢迎大家拍砖,我会及时修正相关内容。
    
        实验一:默认参数不能同时存在于函数声明和函数定义中。
    
    #include <iostream>
    #include <tchar.h>
    using namespace std;
    
    void SetHeight(double dHeight = 183.5);
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight(181.5);
        return 1;
    }
    
    void SetHeight(double dHeight = 183.5)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    然后调整一下顺序(本文的例子只用于实验,并不侧重于代码本身是否有意义):
    
    #include <iostream>
    #include <tchar.h>
    using namespace std;
    
    void SetHeight(double dHeight = 183.5);
    void SetHeight(double dHeight = 183.5)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight(181.5);
        return 1;
    }
    两处代码编译的时候,编译器都会报告如下错误:
    error C2572: 'SetHeight' : redefinition of default parameter : parameter 1,see declaration of 'SetHeight'
    
    实验一小结:编译器不允许默认参数同时存在于声明和定义中,之所以这个做,个人猜测:
    1、没必要,声明和定义都有默认参数相比于声明或者定义中一处有默认参数,没什么多余的意义。
    2、容易犯错,就上例而言,假如在声明中设置默认参数值为183.5,在定义中设置默认参数值为167.5,将导致错误的产生,所以,只允许其中一处设置默认参数可以避免这种形式的错误。
    
        实验二:默认参数的所在的位置需要在调用者的前面
    
    #include <iostream>
    #include <tchar.h>
    using namespace std;
    
    void SetHeight(double dHeight = 183.5);
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight();
        return 1;
    }
    
    void SetHeight(double dHeight)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    上述代码编译通过。
    
    #include <iostream>
    #include <tchar.h>
    using namespace std;
    
    void SetHeight(double dHeight);
    
    void SetHeight(double dHeight = 183.5)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight();
        return 1;
    }
    上述代码编译通过。
    
    #include <iostream>
    #include <tchar.h>
    using namespace std;
    
    void SetHeight(double dHeight = 183.5);
    
    void SetHeight(double dHeight)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight();
        return 1;
    }
    上述代码编译通过。
    
    #include <iostream>
    #include <tchar.h>
    using namespace std;
    
    void SetHeight(double dHeight);
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight();
        return 1;
    }
    
    void SetHeight(double dHeight = 183.5)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    上述代码在编译的时候,编译器报告如下错误:
    error C2660: 'SetHeight' : function does not take 0 parameters
    
    实验二小结:编译通过的代码都有一个共同点,无论默认参数在函数声明语句中还是在函数定义语句中,默认参数的位置都在调用该函数的语句之前(代码的编写顺序),而编译失败的代码是因为调用函数的语句在默认参数所在的语句之后,由此得出一个结论:默认参数的语句所在的位置需要在主调函数的语句的前面。
    
        实验三:函数的声明和定义分别位于不同文件中的默认参数规则
    
    // Head.h
    #pragma once
    #include <tchar.h>
    #include <iostream>
    using namespace std;
    
    void SetHeight(double dHeight = 183.5);
    
    //Body.cpp
    #include "Head.h"
    void SetHeight(double dHeight)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    
    //Main.cpp
    #include "Head.h"
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight();
        return 1;
    }
    上述代码编译通过,因为Main.cpp中包含了Head.h,等价于将Head.h中的内容拷贝到#include"Head.h"的位置,所以在_main函数中的SetHeight()语句执行之前,就找到了带默认参数的函数声明。
    
    // Head.h
    #pragma once
    #include <tchar.h>
    #include <iostream>
    using namespace std;
    
    void SetHeight(double dHeight);
    //Body.cpp
    #include "Head.h"
    
    void SetHeight(double dHeight = 183.5)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    //Main.cpp
    #include "Head.h"
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight();
        return 1;
    }
    上述代码在编译的时候,编译器报告如下错误:
    error C2660: 'SetHeight' : function does not take 0 parameters
    由此,我猜测:编译器在编译到_tmain函数的SetHeight()语句的时候,只找到了SetHeight的声明,并以此判断出函数参数有错误。所以,SetHeight函数的定义部分并未出现在主调函数语句之前。
    
    // Head.h
    #pragma once
    #include <tchar.h>
    #include <iostream>
    using namespace std;
    
    void SetHeight(double dHeight);
    //Body.cpp
    #include "Head.h"
    
    void SetHeight(double dHeight = 183.5)
    {
        cout << _T("身高为:") << dHeight << endl;
    }
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        SetHeight();
        return 1;
    }
    上述代码编译通过。
    
    实验三小结:函数的声明和定义位于不同文件中的默认参数的规则同样要遵循实验二的结论。
    
    上述的三个实验得出的两条结论:
    1、编译器不允许默认参数同时存在于声明和定义中。
    2、默认参数的语句所在的位置需要在主调函数的语句的前面。
    同样适用于类的普通成员函数。
  • 相关阅读:
    中国各城市PM2.5数据间的相关分析
    vue中template的作用及使用
    Vue 点击获得父元素,子元素,兄弟元素(DOM操作)
    解决elementUI中message消息多次重复展现的问题
    Uncaught (in promise) NavigationDuplicated {_name: "NavigationDuplicated"}的解决方法
    v-focus给input设置焦点
    Cannot find name 'template'.Vetur 问题解决
    vue的prototype和拦截器
    Nginx下载启动
    Nginx负载均衡原理
  • 原文地址:https://www.cnblogs.com/fnlingnzb-learner/p/5897572.html
Copyright © 2011-2022 走看看