Jump to content
  • Advertisement
Sign in to follow this  
gah_ribaldi

Output class name of a derived class

This topic is 4001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!