CONVERSION from C to C++ 
Author Message
 CONVERSION from C to C++

is there a site that deals with problems when converting from C to C++;  i need more info on this because i keep
finding errors when converting to C++;  for example, I want to be able to define an enumeration type;  i would then
like to assign a variable to this type;  furthermore I would like to then treat theis variable as an integer;  for
example if the type was Days and contained sunday-> saturday (0 -> 6), I want to be able to define a variable today and
say today=tuesday;  then i would like to say today-1 and have the value of this statement be monday;  for some reason I
could do this in C but not in C++; the help contents say to do some type of dynamic casting;  i did that but i still am
having problems; an example that is similar to my code is:

today=saturday; -> today=6
today=(today -1)/3

any ideas on how I can get my enumeration types to behave like integers?

let me know


-----------------** -- Posted from CodeGuru -- **-----------------
http://www.*-*-*.com/ ;  The website for Visual C++ programmers.



Mon, 09 Jul 2001 03:00:00 GMT  
 CONVERSION from C to C++

Quote:

> is there a site that deals with problems when converting from C to C++;  i need more info on this because i keep
> finding errors when converting to C++;  for example, I want to be able to define an enumeration type;  i would then
> like to assign a variable to this type;  furthermore I would like to then treat theis variable as an integer;  for
> example if the type was Days and contained sunday-> saturday (0 -> 6), I want to be able to define a variable today and
> say today=tuesday;  then i would like to say today-1 and have the value of this statement be monday;  for some reason I
> could do this in C but not in C++; the help contents say to do some type of dynamic casting;  i did that but i still am
> having problems; an example that is similar to my code is:

> today=saturday; -> today=6
> today=(today -1)/3

> any ideas on how I can get my enumeration types to behave like integers?

What you are seeing is a result of C++ being more strongly typed than C.  
In C++, when you define an enum, the enum is treated as a user defined
type which is not the same as int.  To achieve what you explain above,
try the following:

   enum Days
   {
      Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
   };
   Days today;

   today = Friday;
   cout << "Today is " << today << endl;

   today = static_cast<Days>( static_cast<int>(today) - 1 );
   cout << "Today is now " << today << endl;

--
*------------------------------------------------------------
| J. Keith Wedinger
| Technical Specialist - The Limited, Inc.

| http://www.serve.com/uccats
|



Tue, 10 Jul 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. Need C++ text for non cs major course

3. How to show/call Form2.cs from Form1.cs ?

4. Include code in other Cs files

5. Reuse of cs files, namespace, arch advice pls

6. word - automatic numbering/bold/underline/italics

7. How to Generate .cs file at Runtime

8. newbe/cs student, need help w/ code

9. Serial.cs

10. Compile CS source code using ICodeCompiler

11. Two CS files (using namespaces)

12. My .cs files were deleted!?

 

 
Powered by phpBB® Forum Software