Deriving from STL containers

A

Adrian

If I derive from an stl container I rightly get a warning from lint
that deque destructor is not virtual.

Will this cause any problems if do not derive from Container?
Will it clean up properly or do I have to manually call deque
destructor


Adrian.

#include <stdexcept>
#include <deque>

class SomeClass
{

};

typedef std::deque<SomeClass *> list_t;
class Container : private list_t
{
public:
Container() {};
using list_t::pop_front;
using list_t::front;
using list_t::empty;
using list_t::size;
void clear()
{
for(const_iterator i=begin(); i!=end(); ++i)
{
delete (*i);
}
list_t::clear();
};
void push_back(SomeClass * const obj)
{
if(size()>10)
{
throw std::runtime_error("much to big");
}
list_t::push_back(obj);
};

~Container()
{
clear();
}
private:
Container(const Container &);
Container &operator=(const Container &);
};

int main(int argc, char *argv[])
{
Container container;

for(int i=0; i<9; ++i)
{
container.push_back(new SomeClass());
}

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top