Advertisement

C++ special types

Started by February 19, 2002 04:38 PM
3 comments, last by Al MacInnis 22 years, 9 months ago
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
"Everyday above ground is a good day" (Mel in Scarface)
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.

ShadeStorm, the Day_Glo Fish

[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
ShadeStorm, the Day_Glo Fish
Advertisement
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 ?

Thanks for your answer anyway



"Everyday above ground is a good day" (Mel in Scarface)
"Everyday above ground is a good day" (Mel in Scarface)
The best you can do is to overload ++. There is no syntax trick to do what you are describing.
---visit #directxdev on afternet <- not just for directx, despite the name
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

This topic is closed to new replies.

Advertisement