Please an alternative for dynamic_cast/static_cast
Hiya
In my game I have an inventory (which at the moment is a vector of "Items") and I have made all the different types of items derived from that. Now I store my various items in that vector. Each item has a weight, name and description, other items have special variables (for example weapon has damage). How would I access a weapon's attack from the vector. Each item has an enum for its type aswell as a weight, name and description.
At the moment I can only see dynamic/static_cast as an option for this, but I tried that and it said that my weapons attack was 150476 (ok, not that exactly but some random number) although I had set it to 5.
[Edited by - Peter Conn on May 20, 2006 1:26:30 PM]
Quote:In my game I have an inventory (which at the moment is a vector of "Items") and I have made all the different types of items derived from that.
You cannot store objects of derived types into an array (or vector) of base types. Use an array (or vector) of pointers to base types instead.
But I thought that vectors used pointers anyway.
So would it be vector< *Item >.
And how would I get to the infomation?
Weapon *myWeapon = & Inventory?
So would it be vector< *Item >.
And how would I get to the infomation?
Weapon *myWeapon = & Inventory?
A vector copies the object given to it into some location in memory. Internally, it probably does use pointers in some form or another. And then when you reference the object like so data it returns what is probabably a reference (I probably should know that by now).
To access the data, observe:
EDIT: pointer after type
[Edited by - nobodynews on May 21, 2006 8:49:47 AM]
To access the data, observe:
vector<SomeObject*> data;SomeObject * pObject = new SomeObject;data.push_back(pObject); / copies pObject, which contains the location of the SomeObjectpobject->action();data[0]->action();delete pObject;data[0]->action(); // RUN TIME ERROR. You deleted the object, you can't access it!
EDIT: pointer after type
[Edited by - nobodynews on May 21, 2006 8:49:47 AM]
You can use something like COM's IUnknown, or XPCOM's nsISupports.
dynamic_cast doesn't even work in some cases (mix gcc, linux, shared objects, and dlopen() and you'll know what I'm talking about.)
dynamic_cast doesn't even work in some cases (mix gcc, linux, shared objects, and dlopen() and you'll know what I'm talking about.)
If I made the pointer in a function, then stored it in the vector (which is passed to the function by a reference) would there be any problems?
I think the pointer would get deleted, but not the allocated space that one of the vector pointers is pointing to.
Also, how would I access the variables that are only in Weapon then?
Weapon* myWeapon = Inventory;
myWeapon->_attack; ?
I think the pointer would get deleted, but not the allocated space that one of the vector pointers is pointing to.
Also, how would I access the variables that are only in Weapon then?
Weapon* myWeapon = Inventory;
myWeapon->_attack; ?
There wouldn't be any problem, really. You just need to be clear on who "owns" the object pointed to. Is it the container (std::vector in this case), or is it something else?
I recommend you look into boost::ptr_vector< Item > or std::vector< boost::shared_ptr< Item > > to avoid having to deal with lifetime of objects in the case that the container has ownership of the objects.
As for accessing weapons only, you would need to do a dynamic_cast, use home-made RTTI (barf), or rethink your design (what behavior does a weapon require in your design?).
jfl.
I recommend you look into boost::ptr_vector< Item > or std::vector< boost::shared_ptr< Item > > to avoid having to deal with lifetime of objects in the case that the container has ownership of the objects.
As for accessing weapons only, you would need to do a dynamic_cast, use home-made RTTI (barf), or rethink your design (what behavior does a weapon require in your design?).
jfl.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement