Jump to content
  • Advertisement
Sign in to follow this  
Basiror

std::vector operator[]

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

Hi I am just wondering about std::vector operator[] In my latest project i am working on a editor with a decent class layout i have got a CBaseObject from with i derive every object from lights to brushes now i need a dynamic array and people always say use std:vector ok my problem is std:vector returns a reference so i wonder if it is possible to use std:vector like this using namespace std; vector<CBaseObject*> vec; this should create a vector of pointers to the baseobject class and vec should return a pointer to a CBaseObject class so i can cast it to derived classes is this possible? and if so are there any restrictions with it

Share this post


Link to post
Share on other sites
Advertisement
Quote:

vector<CBaseObject*> vec;
this should create a vector of pointers to the baseobject class

and vec should return a pointer to a CBaseObject class so i can cast it to derived classes

is this possible?


Yep that's fine. Note there is also std::list which you should use if you want to sort the data etc.

Share this post


Link to post
Share on other sites
no i just want to store it since it changes everytime

just one thing i forgot to ask

does std::vector delete the memory on his own or shall i do it manually?

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
does std::vector delete the memory on his own or shall i do it manually?

std::vector will detele what IT allocated. YOU are responsible to delete what YOU allocated.

Share this post


Link to post
Share on other sites
If you make a vector<object*> blah you have exactly that - a vector
with pointers to objects - note that these objects _dont_ exist.

So you can do sth like
blah.resize (1)
blah[0] = new object ();

<use blah>


// if you create it yourself, also destrou it yourself
for (all items in blah, iterate)
delete blah;

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
does std::vector delete the memory on his own or shall i do it manually?


Yes it frees it's own memory it allocates itself, but your responsible to delete the memory the raw pointers refer to that you allocated.

I recommend you download boost library and use there smart pointers, they can delete the instances they refer for.

boost's smart pointers

Share this post


Link to post
Share on other sites
thx

ill have a look at this library, but i will propably stick with pure stl without smartpointers since i don t want to mix my sources with tons of custom libs

Share this post


Link to post
Share on other sites
i've never understood that line of thought, if something is usefull then use it, more so in situations like this where one mistake is going to leave memory floating around and where the only addition is ONE header file which gains you ALOT of conviniance, which once you start using you'll wonder how you ever did without it.

Share this post


Link to post
Share on other sites
Also keep in mind that the smart pointer implementation from boost likely to make it into the next version of the C++ standard.

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
vec should return a pointer to a CBaseObject class so i can cast it to derived classes

is this possible?

It's possible but sounds like a bad idea. The idea of putting things into a container together is because they are related and it makes sense to hold them in one place. The virtual functions that you can call on the base class can be overridden in derived classes, so you don't have to do any casting.

If a function only makes sense on a derived class and not on the base then maybe you need to rethink your design.

For example you might have a function called Shatter which is defined on a Window object. It doesn't make sense on PaperBag objects, which have a Burst function instead, so you don't put either of them in the base class. What you should put in the base is a virtual OnDestroy function which can be overriden in the derived classes and they can call the appropriate function:


void Window::OnDestroy() {
Shatter();
Crash();
Tinkle();
}

void PaperBag::OnDestroy() {
Burst();
Bang();
Gust();
}



So, don't do any casting. Make the base class have a useful interface and implement it in the derived classes.

Pete

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!