• Announcements

Archived

This topic is now archived and is closed to further replies.

C++ special types

Recommended Posts

I'm new at C++, and i was wondering how to make a custom type, like angle, that would be an integer like this: 0 1 2 .. 358 359 and if we do a ++ on this value, it would return to 0, like a loop (like the -32767->32767 integer values)... An enum with 1,2,3,4,5,6...,359 would probably function, but isn't there any "better" maner ? Thanks "Everyday above ground is a good day" (Mel in Scarface) Edited by - Al MacInnis on February 19, 2002 5:41:41 PM

Share on other sites
There are 2 topics you want to look up. You need to learn to define "classes", which will give you your new data types, and "operator overloading", which goes hand in hand with classes. Operator overloading is how you tell the compiler what the increment operators mean when used with your new class. Also generally a good idea to overload the assignment operator and the array operator at the same time. (These would be '=' and "[]") If you will be using a LOT of different math on your class, remember to overload each one.

[Edit: I won't actually tell you how because the topics are too complicated for a simple forum. If you like I can come up with examples of how it is done, but it won't answer all your questions. I'd suggest actually looking it up.]

Edited by - ShadeStorm on February 19, 2002 5:52:09 PM

Share on other sites
Oh, i know the operator overloading and classes , but i was wondering: there are not simpler methods to do that, a syntax trick, like, {} in pascal if i remember well ?

"Everyday above ground is a good day" (Mel in Scarface)

Share on other sites
The best you can do is to overload ++. There is no syntax trick to do what you are describing.

Share on other sites
some compilers might let you get away with this:
  #define +++(x) (x > 356)? x++ :x = 0);

but not mvc++.

you could maybe do this:
  #define DecalreMyType(x) int x ## myType#define MyTypeInc(x) ( x ## myType >= 300 )? x ## myType ## = 0: x ## myType ## ++#define MyType(x) x ## myTypeint main(int argc, char* argv[]){ DecalreMyType(z) = 0; MyTypeInc( z ); cout << MyType(z) << endl; cin.get(); return 0;}

but dont expect to be taken seriously...

Edited by - evilcrap on February 20, 2002 3:59:40 PM

Edited by - evilcrap on February 20, 2002 4:00:13 PM

• Partner Spotlight

• Forum Statistics

• Total Topics
627668
• Total Posts
2978540

• 10
• 10
• 10
• 12
• 22