C++ 关于函数定义的问题(参数中的三个点..)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/599744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Question about a function definition (three dots in parameters..)
提问by ashishsony
I came across a function definition:
我遇到了一个函数定义:
char* abc(char *f, ...)
{
}
What do the three dots mean?
三个点是什么意思?
采纳答案by paxdiablo
The ellipses mean that there are a variable number of arguments following. The place you will have used them (perhaps without realising) are the printf
family of functions.
省略号表示后面有可变数量的参数。您将使用它们的地方(也许没有意识到)是printf
函数系列。
They allow you to create functions of that style where the parameters are not known beforehand, and you can use the varargs functions (va_start
, va_arg
and va_end
) to get at the specific arguments.
它们允许您在事先不知道参数的情况下创建该样式的函数,并且您可以使用可变参数函数(va_start
,va_arg
和va_end
)来获取特定参数。
This link herehas a good treatise on the printf
use of variable argument lists.
这个链接here有一篇关于printf
使用变量参数列表的好论文。
回答by Assaf Lavie
Wikipedia on vararg functions in C++.
关于C++ 中 vararg 函数的维基百科。
回答by Assaf Lavie
They are called an elipsis and they mean that the function can take an indeterminate number of parameters. Your function can probably be called like this:
它们被称为省略号,它们意味着该函数可以采用不确定数量的参数。您的函数可能可以这样调用:
abc( "foo", 0 );
abc( "foo", "bar", 0 );
There needs to be a way of indicating the end of the list. This can be done by using the first parameter, as ion a printf(0 format string, or by a special terminator, zero in the example above.
需要有一种方法来指示列表的结尾。这可以通过使用第一个参数来完成,作为一个 printf(0 格式字符串,或者通过一个特殊的终止符,在上面的例子中为零。
Functions with a variable number of parameters are considered bad form in C++, as no type checking or user defined conversions can be performed on the parameters.
参数数量可变的函数在 C++ 中被认为是不良形式,因为不能对参数执行类型检查或用户定义的转换。
回答by Andrew Barrett
This is what is called a varargs function or a variable argument function in C.
这就是 C 中所谓的可变参数函数或可变参数函数。
One you'll probably recognise is printf.
您可能认识的一个是 printf。