Typedefs to mask base types, is this a good idea

Started by
0 comments, last by kuphryn 17 years, 9 months ago
Today it hit me that in order to make my application flexible I should be able to quickly change anything about the classes and code. This led me to think of how to easily change the data types on which my code or classes work on. I decided to have a namespace with a list of typedefs masking the actual data types used with a diffrent name. This is the same principal as functions and defined constants if I decide to switch types I have to change the actual data type a total of 1 time. This approach does has its disadvantages however, when you switch types you may still be calling functions somewhere in code that only operate on the previous type you had listed, giving you an error. There is a diffrent approach which I also concidered which was making a wrapper class, or adding a level of abstraction between the actual datatype and the classes that were ment to interact with them. This seemed alot like reiventing the wheel and a whole lot of overhead so I opted not to go with this method as I dont see any huge advantage over the typedefing method. Is the above mentioned typedefing idea a decent one or does it have unforseen flaws? thanks for the help [smile] EDIT: Example //CLASS A #ifndef _A #define _A #include "Typedefs.h" using namespace Typedef; class A { A(Number nnumber1, nnumber2nB) : number1(nnumber1), number2(nnumber2) Number number1, number2; Number Add(){return number1 + number2;} Number Subtract() {return number1 - number2;} Number Multiply() {return number1 * number2;} } #endif //TYPEDEFS.h #ifndef _TYPE #define _TYPE namespace Typedef { typedef int Number; } #endif As you can see the type Number is theroghly distributed throughout class A and it would take some time to go through and change every instance to a diffrent data type but we dont have to we only have to change this line typedef int Number; to typedef float Number; This accomplished the same basic thing as templates without the massive constraints of having to include the actual code in the .h file and including the type you wish to use for every instance of the class. Templates are better used if you wanted one class that acts on floats ints and doubles but if you want a class that is only ever going to work on ONE of these, you just dont know which one yet, the design I have implemented I belive to be more efficent. Once again feel free to point anything out. [Edited by - raptorstrike on July 3, 2006 2:51:57 PM]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
template

class restriction check out object factory

Kuphryn

This topic is closed to new replies.

Advertisement