Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Using STL, iterators, inheritance, and pointers?

This thread is locked; no one can reply to it. rss feed Print
Using STL, iterators, inheritance, and pointers?
Chris Katko
Member #1,881
January 2002
avatar

First, is it possible to instantiate an array with a base class containing pure virtual functions?

#SelectExpand
1class shape 2{ 3public: 4virtual void draw()=0; 5} 6 7class rectangle 8{ 9public: 10void draw(){ // stuff} 11} 12 13int main() 14{ 15std::list<shape> list_of_shapes; 16}

It seems I'm prevented from using pure virtual functions, which makes sense in the case of an interface. But if I'm making a list of base classes and I never intend for the base class to have functions, why must I use empty non-pure virtual functions? Is this to protect in the accidental/intentional case of me playing an abstract base class into my list and calling an undefined function?

Second, if I create a STL list of those base classes: If I don't use pointers, are they on the stack, and if so, do they not need manually freed? Also, when I push_back, it's a memory copy?

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

jmasterx
Member #11,410
October 2009

It has to be a std::vector<Shape*> because putting things in a vector is like having them on the stack. the vector will malloc by sizeof(object).

When you put anything on the stack, it is non polymorphic because you absolutely need to know the size of something you put on the stack or a vector.

You never need to free anything that goes into a list or vector. By that logic, the pointer itself would not need to be free'd, but the data to which it points does, thus, before you destroy a vector of Shape*, iterate through and call delete or free() on each item.

It you do not use pointers, they are not actually on the stack, but it comes out to the same, essentially, the vector will allocate a big chunk of memory and if your object takes 16 bytes on the stack, it will say, bytes 16 to 32 of my chunk go to item 2 in the array.

Essentially:
Thing& t = *(Thing*)(block[15]);

pkrcel
Member #14,001
February 2012

You meant for Rectangle to inherit from Shape I guess?

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Aikei_c
Member #14,871
January 2013
avatar

You can use shared_ptr http://www.cplusplus.com/reference/memory/shared_ptr/?kw=shared_ptr to do what you want, that is if you want to allocate an object on heap and still don't want to destroy it yourself.
So, if you want to allocate a new shape you would just:

typedef shared_ptr<shape> ShapePtr;
list<ShapePtr> shapesList;
ShapePtr newShape (new rectangle);
shapesList.push_back(newShape);

You can access newShape just like you access a normal pointer. When shared_ptr pointing to it is destroyed, it will be destroyed too. This way you will not have to free it manually.
You should not create several different shared pointers pointing to the same object, if you want another pointer, you need to copy that pointer:

ShapePtr thisShape = shapesList.begin();

It's because shared_ptr counts references to the object and destroys the object only when all shared_ptrs pointed to it are destroyed.
You should also avoid using unnamed shared_ptrs like

shapesList.push_back(ShapePtr (new rectangle));

jmasterx
Member #11,410
October 2009

It should be noted that shared_ptr is a C++11 feature and requires a C++11 compiler.

pkrcel
Member #14,001
February 2012

Or Boost libraries

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Go to: