Sign in to follow this  
gah_ribaldi

Output class name of a derived class

Recommended Posts

Hey all. Here's one that's got me scratching my head in the middle of the night (not general nights, just tonight). I have a class Item which does essentially nothing apart from the fact it has derived classes Potion, Armor and Weapon. (Is there a name for that type of base class?) I have a vector of Items which contains a mixture of the derived classes. Now I have a need to run through the vector, outputting to screen (console) the type of the derived class, so something like: Item Name Item Type HealthPot Potion ManaPot Potion Helmet Armor Bracers Armor Sword Weapon or whatever is inside. wile I'm happy with iterating through and using cout << iter->getName() for the first column, is there a way to output the derived class name to populate the second column, or would I need to have the base class contain a string which is correctly populated with the name in the relevant constructor and then output that?

Share this post


Link to post
Share on other sites
You can use the typeid operator on a dereferenced pointer to get a std::type_info reference. You can then use the name() member function to get a human readable string. However, that name may not be formatted in the manner you want. For example, it might read "class Potion" rather than just "Potion". If you want control of the exact string produced, you can add a virtual member function that returns the string you want want.

Share this post


Link to post
Share on other sites
Quote:
Original post by gah_ribaldi
I have a class Item which does essentially nothing apart from the fact it has derived classes Potion, Armor and Weapon. (Is there a name for that type of base class?)


Sounds like you have an abstract base class (if not, item certainly sounds like it should be abstract).

Quote:

is there a way to output the derived class name to populate the second column, or would I need to have the base class contain a string which is correctly populated with the name in the relevant constructor and then output that?



class Item
{
...
virtual std::string GetTypeName() = 0;
};

class Potion : public Item
{
...
std::string GetTypeName() {return "Potion";}
};




If you do something like that, every class that derives from Item will need to provide its own implementation of GetTypeName, and you can loop over all the elements of your vector and call that.

Share this post


Link to post
Share on other sites
At the moment I'm running with:

//Item.h

class Item
{
private:
std::string itemType;
public:
Item();
~Item();

std::string getItemType(void);
};



then having each constructor have a line populating itemType with the relevant type.

Is there any reason why that'd be a bad way to do it? It ensures I get the output saying exactly what I want, at the cost of it requiring an extra variable, and an extra line of code in each constructor I write...

Share this post


Link to post
Share on other sites
There's no need for the extra member variable in Item. Just declare a virtual member function that returns the name and have each derived class implement it. Ex:

class Item {
public:
Item();
virtual ~Item();

virtual std::string getItemType(void) = 0;
};

class Potion : public Item {
public:
std::string getItemType(void) { return "Potion"; }
};

Also note that classes intended to be base classes should have virtual destructors. Bad things can happen if you don't use them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this