Created
June 16, 2013 03:01
-
-
Save kingsamchen/5790595 to your computer and use it in GitHub Desktop.
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
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