Use the keyword class as a variable name in C++

与世无争的帅哥 提交于 2019-11-30 18:46:28

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' $< > $@
...

#include "python_.hpp"

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!