Base class:
Child class:
or only option is to cast to child class each time launch grenade function is called?
Base class:
I don’t fully understand your issue here but is there anything preventing you from using polymorphism in this scenario? As in have a virtual function such as LaunchAttack() in AEnemy that is overriden in ARanger to launch the grenade. That way you could get rid of the template, hold on to an AEnemy and call this function on it without the downcasting. If it is the case that the different enemies have multiple different attacks then you either have to refactor your approach to something more flexible or indeed use this not-so-elegant approach.
In C++ you can’t change variable type dynamically. So generally you have 3 options:
-Each time cast as you did
-Make your Proprietor a base class type and keep virtual functions in base class, so they can be called directly without casting and overridden in child classes
-Use interfaces, then you can call them directly on any object regardless of class
Ah, i thought maybe it is possible to do it with templated variable, so asked here, who might know what i don’t, thanks for the answer.
I didn’t make it as a virtual function in base class since only one enemy type would use such function, then i guess i will use casting(since it won’t be called lots of times frequently) or just an interface.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.