Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class Base
- {
- public:
- int *f;
- Base():f(new int)
- {
- *f = 12;
- }
- virtual ~Base()
- {
- delete f;
- }
- Base(const Base& p):f(new int)
- {
- *f = *p.f;
- }
- virtual Base* copy()
- {
- return new Base(*this);
- }
- };
- class Child : public Base
- {
- public:
- Child()
- {
- *f = 22;
- }
- ~Child()
- {}
- Child(const Child& p)
- {
- *f = *p.f;
- }
- Child* copy()
- {
- return new Child(*this);
- }
- };
- int main()
- {
- vector<Base*> base;
- Base* b1 = new Base;
- Child* b2 = new Child;
- base.push_back(b1);
- base.push_back(b2);
- vector <Base*> v2;
- for (int i = 0; i < base.size(); i++)
- {
- Base* b = base[i] -> copy();
- v2.push_back(b);
- }
- for (int i = 0; i < base.size(); i++) {
- delete base[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement