I'm porting thousands of lines of legacy C code to C++. One makor issue
I've got is that of enums being treated differently in C and C++.
There's an automatic cast from int to enum in C but not in C++. The
code is full of constructs like this:
enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;
garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */
Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?
--
Simon Elliott http://www.ctsn.co.uk 9 1518
Simon Elliott wrote: I'm porting thousands of lines of legacy C code to C++. One makor issue I've got is that of enums being treated differently in C and C++. There's an automatic cast from int to enum in C but not in C++. The code is full of constructs like this:
enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;
garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */
Do I really have to trawl through all this code and put in many hundreds of casts, or is there something clever I can do to get round this?
The only "clever" thing to do is to keep this C (if it worked before, why
change?)
V
Simon Elliott wrote: I'm porting thousands of lines of legacy C code to C++. One makor issue I've got is that of enums being treated differently in C and C++. There's an automatic cast from int to enum in C but not in C++. The code is full of constructs like this:
enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;
garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */
Do I really have to trawl through all this code and put in many hundreds of casts, or is there something clever I can do to get round this?
Why are you bothering to port C code to C++?* Leave it in C and call
what you need (from C++).
HTH,
--ag
* - No slam on C++ intended; just a practical comment.
--
Artie Gold -- Austin, Texas
"If you don't think it matters, you're not paying attention."
Thierry Miceli posted: enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;
garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */
Do I really have to trawl through all this code and put in many hundreds of casts, or is there something clever I can do to get round this? If you really want to convert the code to C++ you could try to
redefine the '|' operator for the enum. e.g:
enum GardenVegetables { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 };
GardenVegetables operator|(GardenVegetables veg1, GardenVegetables veg2) { return (GardenVegetables) ((int)veg1 | (int)veg2); }
return static_cast<GardenVegetables>( ( static_cast<int>(veg1) |
static_cast<int>(veg2) ) );
C++, not C/C++ ;-D
-JKop
"JKop" <NU**@NULL.NULL> wrote in message news:Pyngd.40348 Thierry Miceli posted: enum GardenVegetables { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 };
GardenVegetables operator|(GardenVegetables veg1, GardenVegetables veg2) { return (GardenVegetables) ((int)veg1 | (int)veg2); }
return static_cast<GardenVegetables>( ( static_cast<int>(veg1) | static_cast<int>(veg2) ) );
C++, not C/C++ ;-D
It seems fine to use C style casts for converting one integer/float type to
another. Imagine that there exists a constructor
GardenVegetables::GardenVegetables(int). Only for converting one
pointer/reference type to another do I use one of the C++ style casts
(usually static_cast, but sometimes reinterpret_cast).
Also, instead of return (GardenVegetables) ((int)veg1 | (int)veg2);
one could say
return GardenVegetables ( (int(veg1) | int(veg2)) );
but neither way is superior, though I'm not sure if this second way is
allowed in C.
Similarly, "(size_t)i" which is equivalent to "size_t(i)" where i is an int,
also seems fine to me as a good style of C++. Similarly, "(size_t)i" which is equivalent to "size_t(i)" where i is
an int, also seems fine to me as a good style of C++.
See but that's why "reinterpret_cast" was given such a horrid name:
float p;
int* k = (int*) &p; //legal
int* k = int*(&p) ; // no can do
To achieve this, you need to be horrid:
int* k = reinterpret_cast<int* const>(&p);
I myself never use (int). Here's how I do it:
Step 1) Use "static_cast". If rejected go to step 2.
Step 2) Use "reinterpret_cast".
I've never had to use "dynamic_cast" so far!
-JKop
-JKop
> > GardenVegetables operator|(GardenVegetables veg1, GardenVegetables veg2) { return (GardenVegetables) ((int)veg1 | (int)veg2); }
return static_cast<GardenVegetables>( ( static_cast<int>(veg1) | static_cast<int>(veg2) ) );
C++, not C/C++ ;-D
I agree, I sacrificed purity for readability.
Thierry
"JKop" <NU**@NULL.NULL> wrote in message news:M7pgd.40397 Similarly, "(size_t)i" which is equivalent to "size_t(i)" where i is an int, also seems fine to me as a good style of C++.
See but that's why "reinterpret_cast" was given such a horrid name:
float p;
int* k = (int*) &p; //legal
int* k = int*(&p) ; // no can do
I agree that for casting between pointer and reference types, it's good to
use static_cast or reinterpret_cast. But for casting between value types, C
style casts which behave like constructors, seem perfectly fine.
To achieve this, you need to be horrid:
int* k = reinterpret_cast<int* const>(&p);
I myself never use (int). Here's how I do it:
Step 1) Use "static_cast". If rejected go to step 2.
Step 2) Use "reinterpret_cast".
Agreed for pointer types only.
I've never had to use "dynamic_cast" so far!
It arises when writing generic code, and you can't or don't want to edit the
base class by continually adding virtual functions to it.
Siemel Naran wrote: I agree that for casting between pointer and reference types, it's good to use static_cast or reinterpret_cast. But for casting between value types, C style casts which behave like constructors, seem perfectly fine.
C++ newer casts are still better.
--
Ioannis Vranos http://www23.brinkster.com/noicys
On 28/10/2004, Simon Elliott wrote:
[snip]
Thanks for all the responses to this. I'm still unsure about how to
proceed because there are lots of different enums which are used in
lots of different ways. With the current compiler (BCB3) I have the
(slightly grubby) option of suppressing the warning and leaving
everything as it is. But it's possible that other compilers won't let
me do this.
--
Simon Elliott http://www.ctsn.co.uk This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: SpaceCowboy |
last post by:
I recently got into a discussion with a co-worker about using enums across a
dll interface. He wanted to use chars instead, argueing that depending on
compiler settings the size of an enum could...
|
by: Faisal |
last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
|
by: Mark A. Gibbs |
last post by:
i have been toying with the idea of making my enums smarter - ie, more
in line with the rest of the language. i haven't tested it yet, but what
i came up with is a template like this:
template...
|
by: SpotNet |
last post by:
Hello CSharpies,
Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of
constant declarations that are UInt32 that I want to put in Enums to ease
the use of Intellisence. I...
|
by: Martin Pritchard |
last post by:
Hi,
I'm working on a project that historically contains around 40 enums. In the
database various fields refer to the int values of these enums, but of
course ref integrity is not enofrced and...
| |
by: Faisal |
last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
|
by: Simon Elliott |
last post by:
I have some legacy C++ code which requires some enums to be 1 or 2
bytes in size.
I'd ideally like to be able to specify that a few carefully selected
enums are a particular size.
By default,...
|
by: Marc Gravell |
last post by:
This one stumped me while refactoring some code to use generics...
Suppose I declare an enum MyEnum {...}
Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ?
Of course,...
|
by: Jon Slaughter |
last post by:
is there a simple way to "step" through enums? I have a button that I want
to click and have it "cycle" through a set of states defined by enums but
the only way I can think of doing this...
|
by: Bob |
last post by:
Hi,
Can someone explain why you can't declare enums in an interface?
The compiler says "interfaces can't declare types"
Ignoring the syntax implications it seems to me that you should be able to...
|
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,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |