std::vector operator[]
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
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.
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?
just one thing i forgot to ask
does std::vector delete the memory on his own or shall i do it manually?
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.
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;
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;
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
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
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
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.
Also keep in mind that the smart pointer implementation from boost likely to make it into the next version of the C++ standard.
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement