Skip to main content
gamedev.net
🔒 Locked

Variable...variable type

Started by MotionCoil 6 replies 600+ views
Original Post
MotionCoil
MotionCoil
Hey all. Ok, my Console and Console class is the central hub for all operations. The logic of the console handles different things like models, lights etc. I was wondering, i have one function to handle the logic, if it is possible to declare one of the arguements as say a generic data type and then depending on the other arguments, the function can work out what the actual type is. Is the generic data type 'void' eg assuming void is generic and ModelObj/LightObj are respective classes: int Logic(bool Model, bool Light, void *Object) { if( Model == true ) ModelObj = Object; else if( Light == true ) LightObj = Object; } is this how you would do it, or is there some other way? is it even possible? Thanks, any help is much appreciated
Bouga
Bouga
Well, you could overload the int Logic(..) with whatever type you need. If you dont know how to do it:
int Logic(CLight *Object){..do something}int Logic(CModel *Object){..do something}void main(){CLight Light;CModel Model;Logic(&Light); //will call the first functionLogic(&Object); //will call the second function}


you could overload the function for every type of parameter you needed.

Or if your light and model classes are inherited from the same base class, you could have the function take a pointer to the base class and have and ID identifier in it, so then the function would just check which type is it. Dont know if any of that is right for your case, though.
"A screen buffer is worth a thousand char's" - me
MotionCoil
MotionCoil
Cheers.
I'm not sure if its working yet but I have tried the void *Object method:

int logic([arguments],void *Object)
{
...
CModel *Model = (CModel*)Object;
...
else
...
CLight *Light = (CLight*)Object;
...
}

havn't tested it yet.

The overloading might be a good alternative.

Thanks for that
antareus
antareus
Use dynamic_cast if you have to determine the type at runtime within a function *and* LightObj/ModelObj have a common base class (Obj?).

Use function overloading if the type is known at runtime (e.g. you aren't storing pointers to base classes in a container somewhere and passing them into the function).
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
mishikel
mishikel
Overloading is a good alternative. Or your CLight and CModel classes could be polymorphic.


class Base
{
virtual void ExecuteLogic() = 0;
};

class CLight : public Base
{
virtual void ExecuteLogic() { ... }
};

class CModel : public Base
{
virtual void ExecuteLogic() { ... }
};


When you find yourself switching on types, you're usually headed down the wrong path.
MotionCoil
MotionCoil
Maybe switching types is going down the wrong path. However, the thing is that the function doesn't just deal with objects but alot of other input. Ergo, its easier just writing one body than lots of different bodies per object.
Still got to test the void method. Then i might look into overloads.

Thanks for the help so far.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.