c:带有函数实现的内联枚举
我遇到过一些 C 代码,其中有一个枚举类型,后跟一个函数实现,例如:
enum OGHRet funcX ( OGH *info, void *data, int size )
{
/* c code that does stuff here */
}
我对这个枚举语句如何与函数实现内联工作感到困惑。 我假设它是 funcX 的返回类型,但为什么用 enum 显式声明?
提前致谢。
I have come across some c code where the there is an enum type followed by a function implementation, such as this:
enum OGHRet funcX ( OGH *info, void *data, int size )
{
/* c code that does stuff here */
}
I am confused over how this enum statement works inline with the function implementation.
I assume it is the return type of funcX, but why is declared explicitly with enum?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它只是说它返回一个名为 OGHRet 的枚举,该枚举将在其他地方定义。
这是一段代码,显示了枚举和并排返回枚举的函数......
its just saying its returning an enum called OGHRet which will be defined elsewhere.
Here's a fragment of code that shows enums and functions that return enums side by side...
如果您像这样定义枚举类型:
那么
enum OGHRet
只是类型的名称。您不能像OGHRet
那样引用该类型;这是一个标签,仅在enum
关键字之后可见。为该类型提供单字名称的唯一方法是使用 typedef ——但实际上没有必要这样做。如果您坚持能够调用类型
OGHRet
而不是enum OGHRet
,您可以这样做:这里声明枚举类型时不带标签,然后 typedef 创建它的别名。
If you define an enum type like this:
then
enum OGHRet
is simply the name of the type. You can't refer to the type just asOGHRet
; that's a tag, which is visible only after theenum
keyword.The only way to have a one-word name for the type is to use a typedef -- but it's really not necessary to do so. If you insist on being able to call the type
OGHRet
rather thanenum OGHRet
, you can do this:Here the enumeration type is declared without a tag, and then the typedef creates an alias for it.
也许是因为它没有声明为 typedef,如下所示:
这里您需要通过
enum OGHRet
来引用它。要仅使用
OGHRet
,您需要这样做:Maybe because it isn't declared as a typedef, like this:
Here you will need to make reference to this by
enum OGHRet
.To use only
OGHRet
, you need to do it like this:您需要
enum
关键字来为OGHRet
提供类型定义。在 C++ 中,可以省略它。另请参阅此问题。
You need the
enum
keyword to provide a type definition forOGHRet
. In C++, you can omit it.See this question, as well.