I have a kindof simple problem I dont know how to solve, its from using python & becoming accustomed with working with variables where the data type doesn't matter . I am working with the windows Task Scheduler & its millions of objects it has, ITask...this ITask...that.
So I have a function, & depending on the parameter triggerType (an enumeration var), the variable trigger will either be of type ITimeTrigger or IBootTrigger ... ugh this is hard to explain in text, if you look at the code below it will be easy to see what my problem is.
Its ALOT easier to understand my issue by looking at my example below:
enum meh { I_WANT_A_INT = 50001, I_WANT_A_FLOAT };
bool foo( meh triggerType )
{
switch ( triggerType )
{
case I_WANT_A_INT:
{
int trigger = 10;
}
break;
case I_WANT_A_FLOAT:
{
float trigger = 10.111;
}
break;
default:
{
double trigger = 11;
}
break;
}
trigger = 5 * trigger ; // Compile error because trigger is not declared
cout << trigger << endl;
}
The solutions I know I can use are:
- I can overload the function & have one for the ITimeTrigger(int) action & another for IBootTrigger(float). This is something I really dont want to do because the function is really long with alot of repeating code between them.
- ITimeTrigger & IBootTrigger both inherit from the same object ITrigger, so I could declare the trigger var outside the switch as ITrigger, then cast to the object I need within the switch. This will work now, but when I
extend this function to schedule a different kind of task trigger will not inherit from ITrigger (win32 semantics again) so this solution wont work.
How can I declare the variable trigger (whose data type will be determined at run time) so I can then work with the var later on in the function?