先看一下C++中的默认参数实现

void Test(int x = 1, int y = 2, int z = 3)
{
    cout << x << ", " << y << ", " << z << endl ;
}
int main(void)
{
    Test() ;
    Test(10) ;
    Test(10, 20) ;
    Test(10, 20, 30) ;
    system("pause") ;
    return 0 ;
}

如何在C中实现这种效果呢?目前只找到一种方法,宏定义,遗憾的是不能使用同一个函数名了

#define Test0() Test(1, 2, 3)
#define Test1(x) Test(x, 2, 3)
#define Test2(x, y) Test(x, y, 3)
#define Test3(x, y, z) Test(x, y, z)
void Test(int x, int y, int z)
{
    cout << x << ", " << y << ", " << z << endl ;
}

相关文章:

  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2021-06-15
  • 2021-07-25
猜你喜欢
  • 2022-01-04
  • 2021-12-22
  • 2022-02-18
  • 2021-12-09
  • 2021-05-15
相关资源
相似解决方案