Skip to content

Instantly share code, notes, and snippets.

@kingsamchen
Created June 16, 2013 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingsamchen/5790595 to your computer and use it in GitHub Desktop.
Save kingsamchen/5790595 to your computer and use it in GitHub Desktop.
class NLComponent
{
public:
virtual NLComponent* clone() const = 0;
};
class TextBlocks : public NLComponent
{
public:
virtual TextBlocks* clone() const
{
cout<<"constructing TextBlocks"<<endl;
return new TextBlocks(*this);
}
};
class Graphics : public NLComponent
{
public:
virtual Graphics* clone() const
{
cout<<"constructing Graphics"<<endl;
return new Graphics(*this);
}
};
int main()
{
TextBlocks text;
Graphics graph;
vector<NLComponent*> components;
list<NLComponent*> lsc;
components.push_back(&text);
components.push_back(&graph);
for (vector<NLComponent*>::iterator it = components.begin(); it != components.end(); ++it)
{
lsc.push_back((*it)->clone());
}
for_each(lsc.begin(), lsc.end(), [&](NLComponent* ptr)
{
delete ptr;
});
_getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment