By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Using invalid variable names in C++

P: n/a
Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...
};

#ifdef __cplusplus
}
#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?
Mar 9 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Juha Nieminen wrote:
Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...
};

#ifdef __cplusplus
}
#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?
No, you are out of luck.

--
Ian Collins.
Mar 9 '07 #2

P: n/a
Juha Nieminen wrote:
Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...
};

#ifdef __cplusplus
}
#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?
It's not 100% legal according to the C++ rules, but I've never seen a
compiler that wouldn't accept it:

#define class klass
#include "yourheaderfile.h"
#undef class

Then use the member under the name 'klass' in your C++ code.

Mar 9 '07 #3

P: n/a
On Mar 9, 5:34 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...

};

#ifdef __cplusplus}

#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?
Actually I could not find anything strange in this code.
extern "C" will process the definition inside as it process under C.
So the name mangling and other features will not be allowed/enbaled
for those definition. Seems there's no problem in using that
structure. I'm not sure whether I interpreted your questoin properly.
Could you please let us know if any error occurs while using this
structure?

Mar 9 '07 #4

P: n/a
On Fri, 09 Mar 2007 02:37:07 -0800, Sarath wrote:
On Mar 9, 5:34 pm, Juha Nieminen <nos...@thanks.invalidwrote:
> Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...

};

#ifdef __cplusplus}

#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?

Actually I could not find anything strange in this code.
extern "C" will process the definition inside as it process under C.
So the name mangling and other features will not be allowed/enbaled
for those definition. Seems there's no problem in using that
structure. I'm not sure whether I interpreted your questoin properly.
Could you please let us know if any error occurs while using this
structure?
The problem arises in referencing the struct member called "class" from
C++ code. Since "class" is a C++ keyword the compiler will presumably
barf on such usage.

--
Lionel B
Mar 9 '07 #5

P: n/a
"Lionel B" <me@privacy.netwrote in message
news:es**********@south.jnrs.ja.net...
On Fri, 09 Mar 2007 02:37:07 -0800, Sarath wrote:
>On Mar 9, 5:34 pm, Juha Nieminen <nos...@thanks.invalidwrote:
>> Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...

};

#ifdef __cplusplus}

#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?

Actually I could not find anything strange in this code.
extern "C" will process the definition inside as it process under C.
So the name mangling and other features will not be allowed/enbaled
for those definition. Seems there's no problem in using that
structure. I'm not sure whether I interpreted your questoin properly.
Could you please let us know if any error occurs while using this
structure?

The problem arises in referencing the struct member called "class" from
C++ code. Since "class" is a C++ keyword the compiler will presumably
barf on such usage.

--
Lionel B
VC .net 2003

extern "C"
{
struct Whatever
{
int class;
};
};

console5.cpp(8) : error C2332: 'class' : missing tag name
console5.cpp(8) : error C2236: unexpected 'class' 'Whatever::__unnamed'
console5.cpp(8) : error C2467: illegal declaration of anonymous 'class'
console5.cpp(8) : error C2027: use of undefined type 'Whatever::__unnamed'
console5.cpp(8) : see declaration of 'Whatever::__unnamed'
Mar 9 '07 #6

P: n/a
On Fri, 09 Mar 2007 02:56:59 -0800, Jim Langston wrote:
"Lionel B" <me@privacy.netwrote in message
news:es**********@south.jnrs.ja.net...
>On Fri, 09 Mar 2007 02:37:07 -0800, Sarath wrote:
>>On Mar 9, 5:34 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Let's assume that you are using an old C library which header file
you can't change and which has something along the lines of:

#ifdef __cplusplus
extern "C" {
#endif

struct Whatever
{
int class;
...

};

#ifdef __cplusplus}

#endif

Let's also assume that you are writing a C++ program where you
simply must use the library. How would you use that struct and
its 'class' member variable in your C++ code? Let's assume that
you can't isolate the code that uses that struct into its own
C module, but that it must be C++.

Is it even possible?

Actually I could not find anything strange in this code.
extern "C" will process the definition inside as it process under C.
So the name mangling and other features will not be allowed/enbaled
for those definition. Seems there's no problem in using that
structure. I'm not sure whether I interpreted your questoin properly.
Could you please let us know if any error occurs while using this
structure?

The problem arises in referencing the struct member called "class" from
C++ code. Since "class" is a C++ keyword the compiler will presumably
barf on such usage.

--
Lionel B

VC .net 2003

extern "C"
{
struct Whatever
{
int class;
};
};

console5.cpp(8) : error C2332: 'class' : missing tag name
console5.cpp(8) : error C2236: unexpected 'class' 'Whatever::__unnamed'
console5.cpp(8) : error C2467: illegal declaration of anonymous 'class'
console5.cpp(8) : error C2027: use of undefined type 'Whatever::__unnamed'
console5.cpp(8) : see declaration of 'Whatever::__unnamed'
Yup...

#define class klass
extern "C"
{
struct Whatever
{
int class;
};
}
#undef class

int main()
{
Whatever obj;
obj.klass = 42; // ok
}

--
Lionel B
Mar 9 '07 #7

P: n/a
Lionel B wrote:
>
The problem arises in referencing the struct member called "class" from
C++ code. Since "class" is a C++ keyword the compiler will presumably
barf on such usage.
The problem is more fundamental: the keyword "class" is used in the
header in a place where it isn't valid, so it is an error. It doesn't
matter whether some other code tries to reference it.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 9 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.