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

2022-07-18,

目录
  • 一、缺省参数概念
  • 二、缺省参数分类
    • ❗ 全缺省参数 ❕
    • ❗ 半缺省参数 ❕
  • 缺省参数的误区
    • 1.滥用缺省参数,损害代码的结构和可读性。
    • 2.多个缺省参数,可能引入逻辑含混的调用方式
    • 3.重载时可能出现二义性
    • 4.函数调用中的精神分裂症

一、缺省参数概念

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

#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。

到此这篇关于c++缺省参数的具体使用的文章就介绍到这了,更多相关c++缺省参数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《C++缺省参数的具体使用.doc》

下载本文的Word格式文档,以方便收藏与打印。