Generics C++

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
I am trying to do a basic Math Conversions class in C++. I have enumerated values declared for each of my sets (Weight{kg,g,lbs}, Angle{degrees,radians}, etc...).

When I call the function as of right now I have 2 calls:
1. setConversion( enum ) which has, as of right now, 4 overloaded versions. This merely sets the value that I will be multiplying by.

2. (inline) convertProcess( structure ) which has 2 overloaded versions - 1 of which takes an array and 1 of which takes a vector<vector<double>>. This merely takes the value that was set from the setConversion() call and multiplies it (via SSE Intrinsics) to each value in the structure.

Instead of having a ton of overloaded members, is there anyway to send a generic enumerated type into the setConversions() function that is type safe (So they don't convert from feet to pounds)?

I don't see anyway around having to do 2 function calls given that this will need to work seamlessly with both array's and vectors.

Thanks,
-Kevin
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Well yes - I could do that; however, I need type safety. Both enum values supplied have to be comparable (Angle and Angle || Weight and Weight). I know how to do such a thing in Java; however, C++ I am not as familiar with certain aspects.

-Kevin
 

Cogman

Lifer
Sep 19, 2000
10,283
135
106
Originally posted by: Gamingphreek
Well yes - I could do that; however, I need type safety. Both enum values supplied have to be comparable (Angle and Angle || Weight and Weight). I know how to do such a thing in Java; however, C++ I am not as familiar with certain aspects.

-Kevin

Templates are type safe. In fact, they throw up more type safety errors then something basic like, say, an int would. If you really need type safety, consider doing something like making a struct for you different types, IE

Radian
{
float value;
};

Degree
{
float value;
};

This will guarantee that a radian could be placed in a function with a degree.
 

squatchman

Member
Apr 1, 2009
50
0
0
Your homework was probably already due and I'm still thinking a template class would be the most idiot proof way of doing it. I'm not sold on the collection of enumerations though. Going that way means having to update your class every time you want to add a new type.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: squatchman
Your homework was probably already due and I'm still thinking a template class would be the most idiot proof way of doing it. I'm not sold on the collection of enumerations though. Going that way means having to update your class every time you want to add a new type.

It isn't homework.

I was trying to find alternate methods, but an enum seemed to be my best bet thus far.