c:带有函数实现的内联枚举

发布于 2024-12-28 13:13:47 字数 245 浏览 2 评论 0原文

我遇到过一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

So尛奶瓶 2025-01-04 13:13:47

它只是说它返回一个名为 OGHRet 的枚举,该枚举将在其他地方定义。

这是一段代码,显示了枚举和并排返回枚举的函数......

enum Blah { Foo,  Bar };

enum Blah TellMeWhy()
{
   return Bar;
}

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 Blah { Foo,  Bar };

enum Blah TellMeWhy()
{
   return Bar;
}
另类 2025-01-04 13:13:47

如果您像这样定义枚举类型:

enum OGHRet { foo, bar };

那么 enum OGHRet 只是类型的名称。您不能像 OGHRet 那样引用该类型;这是一个标签,仅在 enum 关键字之后可见。

为该类型提供单字名称的唯一方法是使用 typedef ——但实际上没有必要这样做。如果您坚持能够调用类型 OGHRet 而不是 enum OGHRet,您可以这样做:

typedef enum { foo, bar } OGHRet;

这里声明枚举类型时不带标签,然后 typedef 创建它的别名。

If you define an enum type like this:

enum OGHRet { foo, bar };

then enum OGHRet is simply the name of the type. You can't refer to the type just as OGHRet; that's a tag, which is visible only after the enum 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 than enum OGHRet, you can do this:

typedef enum { foo, bar } OGHRet;

Here the enumeration type is declared without a tag, and then the typedef creates an alias for it.

蒗幽 2025-01-04 13:13:47

也许是因为它没有声明为 typedef,如下所示:

enum OGHRet
{
 FOO,
 BAR
};

这里您需要通过 enum OGHRet 来引用它。
要仅使用 OGHRet,您需要这样做:

typedef enum _OGHRet
{
 FOO,
 BAR
}OGHRet;

Maybe because it isn't declared as a typedef, like this:

enum OGHRet
{
 FOO,
 BAR
};

Here you will need to make reference to this by enum OGHRet.
To use only OGHRet, you need to do it like this:

typedef enum _OGHRet
{
 FOO,
 BAR
}OGHRet;
君勿笑 2025-01-04 13:13:47

您需要 enum 关键字来为 OGHRet 提供类型定义。在 C++ 中,可以省略它。

另请参阅此问题

You need the enum keyword to provide a type definition for OGHRet. In C++, you can omit it.

See this question, as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文