Many libraries I've seen / used have typedefs to provide fixed-size portable variables like int8, uint8, int16, uint16, etc. that will be the right size regardless of the platform (and C ++ 11 does this with header stdint.h)
After recently using binary file I / O in a small library that I am writing, I see the advantage of using typedef in such a way as to ensure code portability.
However, if I am going to not print "namespace :: uint32" and not use the built-in fundamental types, I can also make the replacement as useful as possible. Therefore, I consider using classes instead of simple typedefs.
These wrapper classes will implement all the normal operators, so they can be used interchangeably with the main type.
For example:
int x = 0;
can be
class intWrapper {
without having to change any code in "// do stuff"
The reason I'm considering this approach, and not just typedefs, is because I already have functions that work with fundamental types, like
std::string numberToString(double toConvert); std::string numberToHexString(double toConvert); int intToXSignificantPlaces(const int& number, unsigned char numberOfSignificantPlaces); bool numbersAreApproximatelyEqual(float tollerance);
Syntactically, it would be better (and more oop) to do the following:
intWrapper.toString(); intWrapper.toHexString();
It would also allow me to implement the bigint classes (int128, etc.) and have both the smaller ones (based on fundamental types) use the same interfaces.
Finally, each shell can have a static instance of max and min itself, so good syntax is possible int32 :: max and int32 :: min.
However, I have a few problems that I would like to address before doing this (since it is mostly syntactic sugar, and these types will be used as a rule, any additional overhead can have a significant impact on performance).
1) Is there an additional function that calls overhead when using someClass.operator + (), someClass.operator- (), etc. only through int a + int b? If so, will the + () operator attachment eliminate ALL of this overhead?
2) All external functions require a primitive type, for example, glVertex3f (float, float, float) cannot simply be passed 3 floatWrapper objects, is there a way to automatically force the compiler to make a floatWrapper for the float? If so, are there any performance implications?
3) Is there any additional memory overhead? I understand (?) That classes with inheritance have some kind of virtual table pointer and therefore use a little more memory (or is it just for virtual functions?), But assuming that these wrapper classes are not inherited from / are not child classes t any additional memory usage using classes instead of fundamental types?
4) Are there any other problems / performance implications that may arise?