By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,169 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,169 IT Pros & Developers. It's quick & easy.

deleting all elements in a STL container of pointers

P: n/a
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();
}

Dec 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ed**********@gmail.com wrote:
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
Dec 17 '05 #2

P: n/a
ed**********@gmail.com wrote:
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
Dec 17 '05 #3

P: n/a
I'd also add Scott Meyers' "Effective STL" and Item 7.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

Dec 17 '05 #4

P: n/a

ed**********@gmail.com wrote:
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;

Dec 18 '05 #5

P: n/a

ed**********@gmail.com wrote:
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.

Dec 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.