473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using invalid variable names in C++

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
7 4501
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Guy Robinson | last post by:
I have the code below which parses an expression string and creates tokens. Can anyone suggest the best of error checking for things like: Valid variable only obj.attribute -whitespace allowed...
4
by: Gerson Kurz | last post by:
I stumbled across this (while using my homebrewn enum class): class test: pass instance = test() setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123) I would've expected some kind of...
12
by: Frank Stephan | last post by:
Hi, im using Struts with nested tags for form validation. So I end up with form elements named like this risikenManuell.praemieNeu risikenManuell.praemieNeu .... I would like to do a...
0
by: Bill | last post by:
Hi Everyone, I am having some difficulty in using the Windows Resource Localization Editor (WinRes.exe) on several Visual Basic .NET forms (.resx files) in two different projects. The editor...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
5
by: hpi | last post by:
Hello, I have a table : Batch It contains fields batchnummer : Number (Long Integer) datum : Date/Time status : Number (Long Integer) nr_records : Number (Long Integer)
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
0
by: Fraser Dickson | last post by:
I am building a web based system using ASP.NET and VB.NET which has to interact with a web service which uses XML WDDX packets. I have been given the XML Packet Specification by the Web Service...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
61
by: Christoph Zwerschke | last post by:
On the page http://wiki.python.org/moin/Python3%2e0Suggestions I noticed an interesting suggestion: "These operators ≤ ≥ ≠ should be added to the language having the following meaning: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.