Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the keyword class as a variable name in 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.

like image 451
Leif Andersen Avatar asked May 15 '10 18:05

Leif Andersen


People also ask

Can we use keyword as a variable name in C?

A variable name can start with the alphabet, and underscore only. It can't start with a digit. No whitespace is allowed within the variable name. A variable name must not be any reserved word or keyword, e.g. int, goto, etc.

Can we use keyword as a variable name?

We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive.

Why can we use keyword as a variable name?

You cannot use keywords as variable names. It's because keywords have predefined meanings.

What Cannot be used as variable name C?

Explanation: Space, comma and $ cannot be used in a variable name.


2 Answers

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"
like image 130
Anycorn Avatar answered Oct 08 '22 06:10

Anycorn


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.

like image 44
GManNickG Avatar answered Oct 08 '22 07:10

GManNickG