deleting all elements in a STL container of pointers

E

edward.birch

Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}
 
?

=?ISO-8859-1?Q?St=E9phane_Wirtel?=

Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}
Sorry for my english,

In your Cleanup function, where do you define the formal parameter of
your template ?

To erase a item collection, you can use a functor.
Look my struct template 'Destroy', with this declaration (and definition).

template <typename T> struct Destroy {
void operator () (T pointer) {
std::cout << "Destroy of this pointer" << std::endl;
delete pointer;
pointer = 0;
}
};

Here, you must to define the type of your container :
The type is detected by the instantiation of your template.

template <typename Container> void CleanUp (Container & pContainer) {
std::for_each (
pContainer.begin (),
pContainer.end (),
Destroy<typename Container::value_type> ());
}

in your main function :

int main (int argc, char **argv) {
std::vector <std::string *> vect;
vect.push_back (new std::string ("Stephane"));
CleanUp (vect);
}

Best regards,

Stephane
 
?

=?ISO-8859-1?Q?St=E9phane_Wirtel?=

Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}
About this subject, I have good advice for you.

Buy the book : "C++ Templates : The Complete Guide"
Product Details

* Hardcover: 552 pages
* Publisher: Addison-Wesley Professional; 1st edition (November 12,
2002)
* Language: English
* ISBN: 0201734842
* Product Dimensions: 9.5 x 7.5 x 1.2 inches
 
A

Axter

Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}

I recommend you use smart pointers in your container instead of raw
pointers.
If you use smart pointers, then you don't have to worry about
explicitly deleting the pointers, because the smart pointer will do
that for you.
You can use the boost::shared_ptr or clone smart pointers like copy_ptr
and cow_ptr.

http://www.boost.org/libs/smart_ptr/shared_ptr.htm

http://code.axter.com/copy_ptr.h
http://code.axter.com/cow_ptr.h

Example usage:
std::vector<boost::shared_ptr<foo> > vFoo;

std::vector<copy_ptr<foo> > vFoo;
 
E

Earl Purple

Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}

The problem is that there can be a problem using Destroy<ContainerType>
as a template function. Although you can use a function in this
situation, it doesn't work with template functions.

So you should use a functor as other posters have suggested.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top