Is it possible to cast a variable where the cast can be a variable amount? Like say I have variable I, and it's an integer. Maybe I want to cast it as a short, or maybe a char, but it depends on the situation. Instead of creating a condition every time I want the program to decide how to cast it, can I put the size I want to cast my I variable as into another variable and simply cast I with the other variable? If I didn't explain that clearly enough, let me know. Basically, I'm just trying to save myself from having to write a whole pile of unnecesary code. If I can pass the casting size to a variable and cast somehow using that variable, that would make things really easy.
In C, you can use a macro for this sort of thing. Code (Text): #define DO_IT_WITH_SIZE(size, var1, var2) \ do {\ var1 = ((size)var2);\ /* other code here */ \ } while (0) int blah() { char a, b; int c, d; DO_IT_WITH_SIZE(char, a, b); DO_IT_WITH_SIZE(int, c, d); } In C++, templates could be used instead: Code (Text): template<typename my_type> static inline my_type doItWithSize(my_type var1) { my_type tmp = var1; /* other code here */ return tmp; } int blah() { char a, b; int c, d; a = doItWithSize(b); c = doItWithSize(d); }