Created
October 14, 2011 13:27
-
-
Save stevedoyle/1287095 to your computer and use it in GitHub Desktop.
Functor for deleting objects in a container using for_each
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T> | |
class DeleteObj | |
{ | |
public: | |
void operator()(T*obj) { delete obj; } | |
}; | |
// Sample usage ... | |
class Foo | |
{ | |
public: | |
Foo(); | |
void doStuff(); | |
}; | |
int main() | |
{ | |
vector<Foo*> foos; | |
for(int i=0; i<10; ++i) | |
foos.push_back(new Foo()); | |
for_each(foos.begin(), foos.end(), DeleteObj<Foo>()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment