C++缺省参数的具体使用

来自:网络
时间:2022-01-09
阅读:
目录

一、缺省参数概念

缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该默认值,否则使用指定的实参

#include<iostream>
using namespace std;
void TestFunc(int a = 0)//参数缺省值
{
    cout << a << endl;
}
int main()
{
    TestFunc();//没有指定实参,使用缺省值
    TestFunc(10);//指定实参,使用实参
    return 0;
}

❗ 有什么用 ❕

比如在 C 语言中有个很苦恼的问题是写栈时,不知道要开多大的空间,之前我们是如果栈为空就先开 4 块空间,之后再以 2 倍走,如果我们明确知道要很大的空间,那么这样就只能一点一点的接近这块空间,就太 low 了。但如果我们使用缺省,明确知道不需要太大时就使用默认的空间大小,明确知道要很大时再传参

#include<iostream>
using namespace std;
namespace WD
{
    struct Stack
    {
        int* a;
        int size;
        int capacity;    
    };
}
using namespace WD;
void StackInit(struct Stack* ps)
{
    ps->a = NULL; 
    ps->capacity = 0;
    ps->size = 0;
}
void StackPush(struct Stack* ps, int x)
{
    if(ps->size == ps->capacity)
    {
        //ps->capacity *= 2;//err
        ps->capacity == 0 ? 4 : ps->capacity * 2;//这里就必须写一个三目
    }
}

void StackInitCpp1(struct Stack* ps, int defaultCP)
{
    ps->a = (int*)malloc(sizeof(int) * defaultCP);
    ps->capacity = 0;
    ps->size = defaultCP;
}
void StackInitCpp2(struct Stack* ps, int defaultCP = 4)//ok
{
    ps->a = (int*)malloc(sizeof(int) * defaultCP);
    ps->capacity = 0;
    ps->size = defaultCP;
}
int main()
{
    //假设明确知道这里至少需要100个数据到st1
    struct Stack st1; 
    StackInitCpp1(&st1, 100);
    //假设不知道st2里需要多少个数据 ———— 希望开小点
    struct Stack st2;  
    StackInitCpp2(&st1);//缺省
    return 0;
}

二、缺省参数分类

❗ 全缺省参数 ❕

void TestFunc(int a = 10, int b = 20, int c = 30)
{
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;
    cout << endl;
}
int main()
{
    //非常灵活,
    TestFunc();
    TestFunc(1);
    TestFunc(1, 2);
    TestFunc(1, 2, 3);    
    //TestFunc(1, , 3);//err,注意它没办法实现b不传,只传a和b,也就是说编译器只能按照顺序传
    return 0;
}

⚠ 注意:

1️⃣ 全缺省参数只支持顺序传参

❗ 半缺省参数 ❕

//void TestFunc(int a, int b = 10, /*int f, - err*/ int c = 20);//err,

void TestFunc(int a, int b = 10, /*int f, int x = y, -> err*/ int c = 20)
{
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;
    cout << endl;
}
int main()
{
    //TestFunc();//err,至少得传一个,这是根据形参有几个非半缺省参数确定的
    TestFunc(1);
    TestFunc(1, 2);
    TestFunc(1, 2, 3);    
    return 0;
}
//a.h
void TestFunc(int a = 10);
//a.cpp
void TestFunc(int a = 20)
{}

⚠ 注意:

  1️⃣ 半缺省参数必须从右往左依次来给出,且不能间隔着给

  2️⃣ 缺省参数不能在函数声明和定义中同时出现

  3️⃣ 缺省值必须是常量或者全局变量

  4️⃣ C 语言不支持缺省

缺省参数的误区

使用缺省参数时应该注意避开下列几种误区。

1.滥用缺省参数,损害代码的结构和可读性。

    void f(bool b=false)
      {
            if (b)
            {
                  file://code of open file
            }
            else
            {
                  file://code of close file
            }
      }

 打开文件和关闭文件在实现代码上没有什么共同点,把两个属于同一类别的函数误认为是实现机制相同,凭空捏造一个参数硬把它们凑在一块,没有什么好处!相反,谁能记得住f(true)代表打开,f()代表关闭呢?况且,f(false)、f()都可以关闭文件,如果调用者混合使用它们就会增加维护上的困难。这种情况下,写成两个独立的函数,非常清晰。

      void Open()
      {
                  file://code of open file
      }
      void Close()
      {
                  file://code of close file
      }

推而广之,如下的做法也值得商榷。

      class CString
      {
      private:
            char * pcData;
      public:
            CString(char * pc=NULL);
      };
      CString::CString(char * pc)
      {
            if (pc==NULL)
            {
                  pcData=new char[1];
                  //...
            }
            else
            {
                  pcData=new char[strlen(pc)+1];
                  //...
            }
      }

这一个更具备迷惑性,“都是构造器嘛,当然写在一块喽。”有人说。非也!应当看到,无参构造器与带char *参数的构造器使用的代码完全分离,并且缺省参数值NULL在设置数据成员时没有任何作用。CString()构造器应改写如下:

      class CString
      {
      private:
            char * pcData;
      public:
            CString();
            CString(char * pc);
      };
      CString::CString()
      {
            pcData=new char[1];
            //...
      }
      CString::CString(char * pc)
      {
            pcData=new char[strlen(pc)+1];
            //...
      }

    总结:
    (1)凡是出现利用缺省参数值作if判断,并且判断后实现代码完全不同的,都应该分拆成两个独立的函数。
    (2)只有缺省参数值在函数体中被无歧视的对待,也就是函数对于任何参数的实现机制都相同时,才可能是合理的。

2.多个缺省参数,可能引入逻辑含混的调用方式

设计一个类,不仅仅是提供给客户代码正确的功能,更重要的是,对不正确的使用方式作力所能及的限制。

      class CPoint
      {
      public:
            int x;
            int y;
            CPoint(int x=0,int y=0)
            {
                  this->x=x;
                  this->y=y;
            }
      };

乍一看,没什么问题。构造CPoint对象时如果不指定x、y的初值,则设为原点坐标。让我们测试一下:

      CPoint pnt1;
      CPoint pnt2(100,100);
      CPoint pnt3(100);      file://[1]

结果发现pnt3的值为(100,0),跑到x轴上去了。对于想绑定两个参数,让它们同时缺省,或者同时不缺省,我们无能为力。但是如果去掉缺省参数,情况就会好转。

      class CPoint
      {
      public:
            int x;
            int y;
            CPoint()
            {
                  x=0;
                  y=0;
            }
            CPoint(int x,int y)
            {
                  this->x=x;
                  this->y=y;
            }
      };

这样,语句[1]就会引发编译错误,提醒使用者。

抬杠的会说:“CPoint pnt3(100);初始化到x轴,本来就是我想要的。”真的吗?那么,请你在你的类文档中明确指出这种独特的调用方法,并且告诉使用者,将点初始化到y轴是CPoint pnt4(0,100);这种不对称的形式。

至于我嘛,self document好了。

3.重载时可能出现二义性

这个简单,随便举个例子:

      void f(int a,int b=0)
      {
      }
      void f(int a)
      {
      }

虽然潜在的模棱两可的状态不是一种错误,然而一旦使出现f(100);这样的代码,潜伏期可就结束了。

4.函数调用中的精神分裂症

Effective C++ 2nd中的条款,为了本篇的完整性加在这里。这种罕见的症状出现的条件是:派生类改写了基类虚函数的缺省参数值。

      class CBase
      {
      public:
            virtual void f(int i=0)
            {
                  cout<<"in CBase "<<i<<endl;
            }
      };
      class CDerive : public CBase
      {
      public:
            virtual void f(int i=100)
            {
                  cout<<"in CDerive "<<i<<endl;
            }
      };
      CDerive d;
      CBase * pb=&d;
      pb->f();      file://[2]

运行后输出:
 in CDerive 0

记住,缺省参数是静态绑定,而虚函数是动态绑定,所以[2]运行的是CDerive::f()的函数体,而使用的缺省值是CBase的0。

返回顶部
顶部