Hello,
I am trying to send objects of characters (people, creatures etc...) to a method: void attack(class * characterObject){} // EACH CHARACTER HAS THIS METHOD
The thing is, I want to send different types of character object references to the method, so this attack method will work on every type of character I pass as an argument to it. Each type of character is made from there own class (people, trolls, whatever...)
obviously the "class" keyword does not work. Is there anything like an "object" keyword or something that will allow any types of objects in?
class Character {
/* what you want to do with a character */
};
class Troll : public Character {
/* Troll specific behavior */
};
class Human : public Character {
/* Human specific behavior */
};
And then you can have a method:
void attack (Character* character);
Which will work for all Characters (be they troll, human elf or whatever).
template < typename CHARACTER_TYPE > void attack( CHARACTER_TYPE* characterObject )
{
characterObject->do_something() ; // EACH CHARACTER HAS THIS METHOD
}
It means "troll inherits from Character". In effect, each troll is also a character. Please look up some resource on Object Oriented Programming, especially in the context of C++ (as it's unfortunately not quite as straight forward as it might be here) for further details.