书法字典:https://www.shufadict.com

C中如何实现C++中的默认参数?

先看一下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 ;
}

posted on 2010-07-14 10:18  翰墨小生  阅读(857)  评论(0编辑  收藏  举报

导航

书法字典:https://www.shufadict.com