Deriving from STL containers

A

Adrian

Lint (rightly I believe) complains that deque's destructor is not
virtual. So will the following code cause any problems?

If I do not derive from Container does that stop problems?
Do I manually have to call deque's destructor from Containers to make
sure?


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;
}
 
V

Victor Bazarov

Adrian said:
Lint (rightly I believe) complains that deque's destructor is not
virtual. So will the following code cause any problems?
No.

If I do not derive from Container does that stop problems?

What problems?
Do I manually have to call deque's destructor from Containers to make
sure?

Absolutely not.
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;

It is possible that you're going to leak memory if you allow
pop_front without deleting the element.
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[])

If you don't use 'argc' and 'argv', don't declare them.
{
Container container;

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

return 0;
}

V
 
Z

Zeppe

Adrian said:
Lint (rightly I believe) complains that deque's destructor is not
virtual. So will the following code cause any problems?

If I do not derive from Container does that stop problems?

actually, quite the opposite. If you *do* public-derive from Container,
some problem can start to happen, due to the destructor of the child not
being called (you should then declare virtual the destructor of
COntainer). as long as you derive private, I think, there is no problem
at all.

Regards,

Zeppe
 
P

Pete Becker

Adrian said:
Lint (rightly I believe) complains that deque's destructor is not
virtual. So will the following code cause any problems?

No. The danger with a non-virtual destructor is that if you use new to
create an object of the derived type and subsequently delete it through
a pointer to the base type you get undefined behavior. Since your code
doesn't do either of those, it's fine. If you used your Container type
differently it might not be.
If I do not derive from Container does that stop problems?

Yes, certainly. If you don't have a derived type then you can't delete
an object of that type through a pointer to its base.
Do I manually have to call deque's destructor from Containers to make
sure?

No. There are very few situations where it's apropriate to explicitly
call a base class's destructor. Don't mess with that. If you delete an
object of type Container through a pointer to its base the behavior is
undefined. Nothing more can be said.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
P

Pete Becker

Pete said:
Yes, certainly. If you don't have a derived type then you can't delete
an object of that type through a pointer to its base.

Whoops, sorry: that was a more subtle question than I thought. No,
deriving from Container doesn't affect the possible problem.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 

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,880
Messages
2,569,944
Members
46,249
Latest member
MelodyThye

Latest Threads

Top