在 C++ 中使用关键字 class 作为变量名

发布于 2024-09-01 20:24:01 字数 345 浏览 4 评论 0原文

我在编写使用为 C 文件设计的头文件的 C++ 代码时遇到问题。特别是,头文件使用了一个名为 class 的变量名:

int BPY_class_validate(const char *class_type, PyObject *class, PyObject *base_class, BPY_class_attr_check* class_attrs, PyObject **py_class_attrs);

这在 C 中有效,因为 class 不被视为关键字,但在 C++ 中,class 是。那么我是否可以将这个头文件 #include 到 c++ 文件中,或者我运气不好?

谢谢。

I am having trouble writing C++ code that uses a header file designed for a C file. In particular, the header file used a variable name called class:

int BPY_class_validate(const char *class_type, PyObject *class, PyObject *base_class, BPY_class_attr_check* class_attrs, PyObject **py_class_attrs);

This works in C as class isn't taken as a keyword, but in C++, class is. So is there anyway I can #include this header file into a c++ file, or am I out of luck?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

盛夏已如深秋| 2024-09-08 20:24:01

尝试这样的事情:

#define class class_variable
// if class is only used in declarations, you can also do
// #define class
#include "c.h"
#undef class

它可能会导致问题,但也许值得一试

(使用 Makefile):

python_.hpp: /usr/include/python.h
    perl -pe 's/\Wclass\W//g' 
lt; > $@
...

#include "python_.hpp"

try something like this:

#define class class_variable
// if class is only used in declarations, you can also do
// #define class
#include "c.h"
#undef class

it may cause problems, but maybe worth a shot

alternative (using Makefile):

python_.hpp: /usr/include/python.h
    perl -pe 's/\Wclass\W//g' 
lt; > $@
...

#include "python_.hpp"
好多鱼好多余 2024-09-08 20:24:01

如果这只是声明,那么变量名称根本不重要。您可以完全删除它们或按照您的意愿更改它们。这是因为声明仅定义了函数的名称和类型,即:

int BPY_class_validate(const char *, PyObject *, PyObject *,
                        BPY_class_attr_check*, PyObject **);

但是如果您想要名称(更具描述性),您可以在所拥有的内容末尾添加下划线:

int BPY_class_validate(const char *class_type, PyObject *class_,
                        PyObject *base_class, BPY_class_attr_check* class_attrs, 
                        PyObject **py_class_attrs);

这不会'不要破坏任何其他代码。

If this is declaration only, then the variable names don't matter at all. You can completely remove them or change them how you please. This is because the declaration merely defines the name and type of the function, which is:

int BPY_class_validate(const char *, PyObject *, PyObject *,
                        BPY_class_attr_check*, PyObject **);

But if you want the names (to be a bit more descriptive), you can just throw an underscore at the end of what you have:

int BPY_class_validate(const char *class_type, PyObject *class_,
                        PyObject *base_class, BPY_class_attr_check* class_attrs, 
                        PyObject **py_class_attrs);

This won't break any other code.

じее 2024-09-08 20:24:01

您始终可以使用 #ifdef __cplusplus 在该标头中创建特定于 C++ 的声明

You can always use #ifdef __cplusplus to create a declaration specific for C++ in that header

梦行七里 2024-09-08 20:24:01

您需要修改标头以将“class”替换为其他内容,以便将其编译为 C++。

You will need to modify the header to replace 'class' with something else in order to compile it as C++.

孤者何惧 2024-09-08 20:24:01

实际上,你运气不好。 “class”是一个保留字,您不能将其用作变量标识符。

我想你可以做一些预处理器技巧,比如

#define class othername

但实际上这也很愚蠢,因为它会让你的代码变得混乱并阻止你使用真正的类。

硬着头皮将参数重命名为“theclass”之类的。

As a practical matter, you're out of luck. "class" is a reserved word, you can't use it as a variable identifier.

I suppose you could do preprocessor tricks, like

#define class othername

But really that's silly, too, because it'll make your code confusing and prevent you from using real classes.

Just bite the bullet and rename the parameter 'theclass' or something.

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