9
I have a factory method class that generates "Items" and returns a pointer to the Item that it creates.
I have derived classes of Item. For example, Item can be a "weapon" "consumable" or "armor."
I need to detect which type of Item is created so that I can properly cast the Item to that type. I did some test lines and it looks like it's doing what I want, except for adding a number associated with the type.
Example:
I had the line:
cout << "Type Created: " << typeid(pItem).name() << endl;
Which return the base class Item, but it would display: "4Item"
I then changed that to:
cout << "Type Created: " << typeid(*pItem).name() << endl;
Which would give me the correct derived type, but also throw in that number. So I get something like "5Armor"
Why did pItem return the base class? And why does it return an int with the type? How can I remove the int?
A solution that does what I want - In a "cheating" kind of way:
string type;
string returnedType = typeid(*pItem1).name();
for (int i=1; i < returnedType.length(); i++){
type.push_back(returnedType[i]);
}
cout << "type: " << type << endl;
Thanks