Jump to content
  • Advertisement
Sign in to follow this  
Peter Conn

Please an alternative for dynamic_cast/static_cast

This topic is 4441 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

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 SomeObject
pobject->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]

Share this post


Link to post
Share on other sites
The * goes after the type. And vectors generally use a dynamically allocated c-style array.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.)

Share this post


Link to post
Share on other sites
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; ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!