Consider the following class:
class Foo
{
public:
#ifdef CONDITION
int x = 0;
#endif
int y;
int foo() {
#ifdef CONDITION
return ++x;
#else
return 0;
#endif
}
}
int x
only exists when I define CONDITION
- either through a #define CONDITION
or as a preprocessor definition (-D CONDITION
)
This has the neat advantage that I can't compile it I use x
by mistake somewhere when CONDITION
isn't defined.
For example: If, by mistake, I write something like:
Foo f;
f.x = 10;
This will not be allowed to compile when I'm missing -D CONDITION
However, we get all sorts of nasty problems when class Foo
is declared in a header that is used by multiple projects, where preprocessor definitions differ:
The offset of y
within Foo
will be different, resulting in different interpretations of how an object of Foo
looks.
The question:
Is there some way in which I can declare x
for anyone using Foo
, but still get some sort of compiler warning/error when I try to use it without defining CONDITION
?
Aucun commentaire:
Enregistrer un commentaire