Say i have three classes one called character, one ork and one human. Human and ork are both derived from character. Is there anyway to create a function of type character and inside that function be able to return objects created from either the ork or human classes. Or maybe there is another way of returning either ork or human objects from the same function.
Edit: Nevermind folks it turns out when i tried this earlier i accidentally derived ork and human from a different class. It is in fact possible to return a derived class from a function of base type. Just to be clear this is what i was doing.
character game::getPlayer(char symbol) {
switch (symbol) {
case 'E':
return elfPlayer;
break;
case 'G':
return guardPlayer;
break;
case 'K':
return knightPlayer;
break;
case 'R':
return roguePlayer;
break;
case 'W':
return wizardPlayer;
break;
}
}
If i pictured correctly , u need one pure virtual function inside the base class Character and entire function would be used in derived classes in Human and Orc.
class Character
{
public:
Character();
virtual ~Character();
virtual HRESULT Characterfunc() = 0;
private:
}
and then in derived classes:
class Ork : public Character
{
public:
Ork();
HRESULT Characterfunc();
private:
}
And the last one:
class Human : public Character
{
public:
Human();
HRESULT Characterfunc();
private:
}