为什么"man 2开放"说有两种开放? [英] Why does 'man 2 open' say that there are two kinds of open?

查看:86
本文介绍了为什么"man 2开放"说有两种开放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在输入man 2 open时遇到了这个问题.它说有两种开放,一种有两个参数,一种有三种!上一次我检查我们无法在C中重载函数.它们是怎么做到的?他们是用C ++编写的吗?

I ran into this question while typing man 2 open. It says that there are two kinds of open, one with two args, and one with three! last time i checked we could not overload functions in C. How did they do this? did they write in C++?

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

推荐答案

不,他们只是使用可变参数功能.

No, they just used variadic function.

int open(const char * pathname, int flags, ...);

这使最后一个参数mode为可选.原型仅显示应如何使用该功能,而不显示实际接口.

This makes the last argument mode optional. The prototypes only show how the function should be used, not the actual interface.

当然,与真正的重载不同,编译器无法对mode参数进行类型检查,因此用户必须格外小心,以确保仅传递2或3个参数,并且第3个参数必须为mode_t

Of course, unlike real overloading, the compiler cannot type-check the mode argument, so the user have to be extra careful to ensure only 2 or 3 arguments are passed, and the 3rd argument must be a mode_t.

BTW,如果您检查BSD(包括OS X)的man 2 open,则会显示

BTW, if you check the man 2 open for BSD (including OS X) it shows the correct prototype as above.

这篇关于为什么"man 2开放"说有两种开放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆