Armand Lambrechts
Armand Lambrechts

Reputation: 11

Undefine a defined header file / C++

Is it possible to undefine a header file which is already defined, from another header file?

I'm working with different classes and I need to undefine a certain class in order to change the private part to public.

I know it is not common to change a private to a public class but it could really help me out.

#undef "player.h"
#define private public
#include "player.h"
#undef private

Upvotes: 0

Views: 598

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

There is no way to redefine a class once it is defined. If you try, you will get an error message along these lines

   error: redefinition of 'class player'
     class player {
           ^

Even redefining private into public before, e.g.

#define private public
#include "player.h"
#undef private

is dangerous and violates the contract for this class.

You should rather think about why this seems necessary, and refactor the class if possible.

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234685

Firstly, there are no preprocessor techniques that you can employ that remove statements from the code that's submitted to the compiler.

Secondly, C++ does not allow you to #define private as public. C++11 17.6.4.3.1 Macro names [macro.names]

A translation unit shall not #define or #undef names lexically identical to keywords.

So don't do this.

The only thing you can really do in this situation is to retype the class.

Upvotes: 3

Related Questions