Sign in to follow this  
Basiror

std::vector operator[]

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[i] 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
Quote:

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

and vec[i] 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
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[i];

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
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
Quote:
Original post by Basiror
vec[i] 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
About the smart pointers:
- i am going to need a scene management class in my editor so i can add the deallocation for each object into this class *it stores the root of the scene anyways

to the class hierarchy:
- i just want to keep the possibility but in general i won t need a single cast
- my hierarchy builds on the top of CBaseObject which is the basic representation of everything you can manipulate,create and destroy inside the editor
its pretty similar to the 3dsmax class layout you have got a CNode class and tons of derived classes


about std:vector

just wondering how does std:vector store the objects internally?
stacks or linked lists? or lists of stacks ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
about std:vector

just wondering how does std:vector store the objects internally?
stacks or linked lists? or lists of stacks ?
As a dynamically allocated array. Nothing more. Whenever you try to add beyond how much size is currently allocated, it will allocate a new memory segment, larger than what you currently have, copy all the current data to the new memory segment, add the new element, and delete [] the old memory. It tends to double the amount of memory it allocates, each time it resizes, so that it doesn't need to reallocate and copy very often.

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
just wondering how does std:vector store the objects internally?
stacks or linked lists? or lists of stacks ?


The internal storage of a vector is guaranteed to be a contiguous unpadded block*. In practice this means that vector uses a single array internally. When the vector grows beyond its capacity a new array is created and the contents of the old array are copied over. You can use resize to avoid unnecessary reallocations.

Enigma

*Well, a few years ago it was "about to be guaranteed", so I guess they must have added it to the standard by now!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this