Conflicting class keyword

K

kiran kumr

Hi,

we have the following scenario:

1. A header file that is used in multiple modules a c application and a C++
application.
2. This file has a variable named class[SIZE].
3. How do I declare class now without conflicting with the other variable
class in the common header file.

I cannot rename the variable class and getway without including the header
file.

-Regards,
 
J

Jacek Dziedzic

kiran said:
Hi,

we have the following scenario:

1. A header file that is used in multiple modules a c application and a C++
application.
2. This file has a variable named class[SIZE].
3. How do I declare class now without conflicting with the other variable
class in the common header file.

I cannot rename the variable class and getway without including the header
file.

Perhaps

#undef class
#define class old_class
#include "old_header.h"
#undef class

Does that count as renaming the variable?

I think this should work, although AFAIK it is illegal to re#define
C++ keywords.

HTH,
- J.
 
J

Jim Langston

kiran kumr said:
Hi,

we have the following scenario:

1. A header file that is used in multiple modules a c application and a
C++
application.
2. This file has a variable named class[SIZE].
3. How do I declare class now without conflicting with the other variable
class in the common header file.

I cannot rename the variable class and getway without including the
header
file.

-Regards,

If each compiliation unit is to have it's own copy of the variable then make
it static.
static whatever class[SIZE];

If each compiliation unit is to share the one instance of the variable, then
declare it external:
extern whatever class[SIZE];

and in one, and only one, compilation unit (one of the .cpp files) declare
it as a normal global variable:
whatever class[SIZE];
 
C

Colander

Jim said:
kiran kumr said:
Hi,

we have the following scenario:

1. A header file that is used in multiple modules a c application and a
C++
application.
2. This file has a variable named class[SIZE].
3. How do I declare class now without conflicting with the other variable
class in the common header file.

I cannot rename the variable class and getway without including the
header
file.

-Regards,

If each compiliation unit is to have it's own copy of the variable then make
it static.
static whatever class[SIZE];

If each compiliation unit is to share the one instance of the variable, then
declare it external:
extern whatever class[SIZE];

and in one, and only one, compilation unit (one of the .cpp files) declare
it as a normal global variable:
whatever class[SIZE];

I think you overlooked that in C++ class is an keyword, in C it isn't.

so Legal c code:
int class = 42;
And illegal C++ code:
int class = 42;

Yes there are things that C can do that C++ can't...
 
B

BobR

Colander wrote in message ...
Jim said:
If each compiliation unit is to have it's own copy of the variable then make
it static.
static whatever class[SIZE];
If each compiliation unit is to share the one instance of the variable, then
declare it external:
extern whatever class[SIZE];
and in one, and only one, compilation unit (one of the .cpp files) declare
it as a normal global variable:
whatever class[SIZE];

I think you overlooked that in C++ class is an keyword, in C it isn't.

so Legal c code:
int class = 42;
And illegal C++ code:
int class = 42;

Yes there are things that C can do that C++ can't...

So, in C, this is legal?

int struct = 42;
 
J

Jim Langston

BobR said:
Colander wrote in message ...
Jim said:
If each compiliation unit is to have it's own copy of the variable then make
it static.
static whatever class[SIZE];
If each compiliation unit is to share the one instance of the variable, then
declare it external:
extern whatever class[SIZE];
and in one, and only one, compilation unit (one of the .cpp files)
declare
it as a normal global variable:
whatever class[SIZE];

I think you overlooked that in C++ class is an keyword, in C it isn't.

so Legal c code:
int class = 42;
And illegal C++ code:
int class = 42;

Yes there are things that C can do that C++ can't...

So, in C, this is legal?

int struct = 42;

You're absolutely right, I just typed in the OP's variable name on auto
pilot. Yeah, can't use any keyword as a var name (including struct).

So, no, int struct = 42; is not legal in c++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top