Jump to content
  • Advertisement
Sign in to follow this  

Typedefs to mask base types, is this a good idea

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!