Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The C++ 'new' keyword and C [duplicate]

Possible Duplicate:
Use the keyword class as a variable name in C++

In a C header file of a library I'm using one of the variables is named 'new'. Unfortunately, I'm using this library in a C++ project and the occurence of 'new' as a variable names freaks out the compiler. I'm already using extern "C" { #include<...> }, but that doesn't seem to help in this respect.

Do I have to aks the library developer to change the name of that variable even though from his perspective, as a C developer, the code is absolutely fine, as 'new' is not a C keyword?

like image 253
Florian Avatar asked Jun 02 '10 06:06

Florian


People also ask

What is new keyword in C?

The new operator creates a new instance of a type. You can also use the new keyword as a member declaration modifier or a generic type constraint.

Does new keyword work in C?

No, new and delete are not supported in C.

What does new [] do in C++?

The new operator will create arrays of objects only if the class has a default constructor. Specifies the bounds of an array.

What are keywords in C++?

Keywords (also known as reserved words) have special meaning to the C++ compiler and are always written or typed in short(lower) cases. Keywords are words that the language uses for a special purpose, such as void, int, public, etc. It can't be used for a variable name or function name.


1 Answers

Before including the header file, use the preprocessor to rename new:

#define new mynew
#include <...>
#undef new

That will allow the compilation to proceed.

Do you actually need to access this variable? If not - then you're done. If you do, then you'll need to ensure the .c files for the library are compiled with

-Dnew=mynew
like image 187
psmears Avatar answered Oct 26 '22 01:10

psmears