-
Downcasting issues
Hey guys, I'm trying to make an RPG game, and I'm having problems with downcasting. So I've got a class, Item, and a couple inherited classs, Weapon / Armor (two different classes). Now, I've tried the following statements, and have received the following results:
Original Weapon:
Name - A Sword 1d2 } Attributes of the
Appearance - '|' } Item Class
note: the name is a string, that is created by a method in the weapon class (based on the weapon type, die and sides)
Type 0 }
Sides - 2 } Attributes of the
Die - 1 } Weapon Class
Item* i = &t->item.at(0); // This will return an item, that we know (in these testing cases) is a weapon
::Weapon* w1 = (Weapon*)i;
// Results
// Name: A Sword 1d2
// Appearance: '|'
// Type: - 4429968
// Sides: 0
// Die: -842203136
::Weapon* w2 = i;
// Results
// Error, doesn't work
::Weapon* w3 = dynamic_cast<Weapon*>(i);
// Results
// Name: Expression cannot be evaluated
// Appearance: Expression cannot be evaluated
// Type: Expression cannot be evaluated
// Sides: Expression cannot be evaluated
// Die: Expression cannot be evaluated
::Weapon* w4 = static_cast<Weapon*>(i);
// Results
// Name: A Sword 1d2
// Appearance: '|'
// Type: - 4429968
// Sides: 0
// Die: -842203136
::Weapon* w5 = reinterpret_cast<Weapon*>(i);
// Results
// Name: A Sword 1d2
// Appearance: '|'
// Type: - 4429968
// Sides: 0
// Die: -842203136
The weapon inherits from an item,
class Weapon : public Item
{
...
};
and the Item is just some normal class,
class Item
{
...
};
I'm a huge noob to C++, I come from C# instead. Could anybody help me out with this and tell me what's going on, and what I can do to fix this? It'd be greatly appreciated, thanks so much (btw, I've asked this on another forum board, and someone had suggested writing all weapon and item types to a text file, and reading the information there - although I do appreciate his/her help, I would much rather keep the information in here for now). Thanks guys.