【发布时间】:2019-02-10 11:49:50
【问题描述】:
我有一个接受两个具有默认值的整数的函数,有没有办法让函数的调用者传递任意数量的参数? (第一但不是第二,第二但不是第一,两者都有)。 示例:
void do_something(int first = 0, int second = 0);
int main()
{
do_something(1); // first - how to declare it's the first argument
do_something(1); // second
do_something(1,1);
do_something();
return 0; // I want to allow all those options
}
【问题讨论】:
-
do_something(1); // second- 这是不可能的。您必须明确传递默认参数:do_something(0, 1);。其余的都很好。 -
@Yksisarvinen 你可以写这个作为答案。
标签: c++ parameter-passing optional-parameters default-parameters