Please an alternative for dynamic_cast/static_cast

Started by
11 comments, last by nobodynews 17 years, 11 months ago
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]
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Advertisement
Store pointers in your vector, then it will work.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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?
Whether you think you can or think you can’t, you’re probably right – Henry Ford
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:
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]

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

The * goes after the type. And vectors generally use a dynamically allocated c-style array.
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.)
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; ?
Whether you think you can or think you can’t, you’re probably right – Henry Ford
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.
Thanks

I don't like the idea of rethinking my design, because otherwise my item class will have values for attack, defense and other things which would make it clumbersome.
What are the downsides of using dynamic_cast? I've heard its pure evil although I don't really know why.
Whether you think you can or think you can’t, you’re probably right – Henry Ford

This topic is closed to new replies.

Advertisement