它是在C使用一个枚举参数的公共API函数是个好主意? [英] Is it a good idea to use an enum parameter in public API function in C?

查看:122
本文介绍了它是在C使用一个枚举参数的公共API函数是个好主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个C API除其他外必须提供一种方法来设置一些双值选项。为了确定我用下面的枚举的选项:

I am designing a C API which among other things has to provide a way to set some double-valued options. To identify the options I use the following enum:

typedef enum
{
    OptionA,
    OptionB,
    ...
} Option;

这是个好主意,用选项作为一个公共的API函数的参数类型:

Is it a good idea to use Option as a parameter type in a public API function:

int set_option(Option opt, double value);

或者是更好地使用 INT 而不是:

int set_option(int opt, double value);

考虑到我可能需要在未来增加更多的选择?

considering that I may need to add more options in the future?

也在那里,证明这两种方法现有API的任何好的例子?

Also are there any good examples of existing APIs that demonstrate either approach?

推荐答案

使用枚举你有效地帮助你的函数的用户知道哪些有效的值可用。
当然,缺点是,每当添加新的选项,你需要修改头,因此用户可能需要重新编译自己的code,而如果它是一个 INT 他可能没有做到这一点。

By using an enum you are effectively helping the user of your function to know what valid values are available. Of course the drawback is that whenever a new option is added you will need to modify the header and thus the user may need to recompile his code whereas if it is an int he may not have to do that.

这篇关于它是在C使用一个枚举参数的公共API函数是个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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