Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我在linux手册页中找到了重载函数_C_Overloading - Fatal编程技术网

C 我在linux手册页中找到了重载函数

C 我在linux手册页中找到了重载函数,c,overloading,C,Overloading,当我打开man 2时,我得到了以下信息: int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode); int openat(int dirfd, const char *pathname, int flags); int openat(

当我打开man 2时,我得到了以下信息:

   int open(const char *pathname, int flags);
   int open(const char *pathname, int flags, mode_t mode);

   int creat(const char *pathname, mode_t mode);

   int openat(int dirfd, const char *pathname, int flags);
   int openat(int dirfd, const char *pathname, int flags, mode_t mode);

这很像函数重载。但据说C根本不支持函数重载。那么这里的神奇之处是什么呢?

这些函数并不是真正的多个函数,只是一个单一的、可接受的函数,它允许“重载”,即您可以使用或不使用它们的最终参数调用它们。例如,我的系统上
openat
的实际声明(省略属性标签等)是:


最后的
..
意味着它可以通过
stdarg.h
的变量参数API(
va_start
/
va_arg
/
va_end
)接受其他参数。

要查看这些函数的实际定义,请运行以下命令:

echo "#include <stdio.h>" | gcc -E - | grep -C5 "open"
这是一个典型的varargs函数,如
printf
,但作为程序员,您应该只传递一个
mode\u t
类型的参数,或者根本不传递任何参数。

请参见此说明。
echo "#include <stdio.h>" | gcc -E - | grep -C5 "open"
int open(const char *, int, ...);