#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Base {
public:
Base(const string& s) {
m_s = s;
}
virtual ~Base() {
};
friend ostream & operator <<(ostream & os, const Base & b) {
b.print(os);
return os;
}
virtual void print(ostream & os) const {
os << "Base";
}
string m_s;
};
class Derived : public Base {
public:
Derived(const string& s, int i) : Base(s) {
m_int = i;
}
virtual void print(ostream & os) const {
os << "Derived";
}
int m_int;
};
class Store {
public:
Store(const string& s) {
m_s = s;
}
void Add(const Base& b) {
cout << b << endl; //output before storing to vector
v.push_back(&b);
}
friend ostream & operator <<(ostream & os, const Store & x);
string m_s;
vector<const Base*> v;
};
ostream & operator <<(ostream & os, const Store & x) {
for (auto it = x.v.cbegin(); it != x.v.cend(); ++it) {
cout << *(*it) << endl;
}
return os;
}
int main() {
Store a("aaaaaaa");
a.Add(Derived("abc", 5));
a.Add(Derived("def", 6));
cout << endl;
cout << a; //output after storing to vector
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNsYXNzIEJhc2UgewpwdWJsaWM6CgogICAgQmFzZShjb25zdCBzdHJpbmcmIHMpIHsKICAgICAgICBtX3MgPSBzOwogICAgfQoKICAgIHZpcnR1YWwgfkJhc2UoKSB7CiAgICB9OwoKICAgIGZyaWVuZCBvc3RyZWFtICYgb3BlcmF0b3IgPDwob3N0cmVhbSAmIG9zLCBjb25zdCBCYXNlICYgYikgewogICAgICAgIGIucHJpbnQob3MpOwogICAgICAgIHJldHVybiBvczsKICAgIH0KCiAgICB2aXJ0dWFsIHZvaWQgcHJpbnQob3N0cmVhbSAmIG9zKSBjb25zdCB7CiAgICAgICAgb3MgPDwgIkJhc2UiOwogICAgfQoKICAgIHN0cmluZyBtX3M7Cn07CgpjbGFzcyBEZXJpdmVkIDogcHVibGljIEJhc2UgewpwdWJsaWM6CgogICAgRGVyaXZlZChjb25zdCBzdHJpbmcmIHMsIGludCBpKSA6IEJhc2UocykgewogICAgICAgIG1faW50ID0gaTsKICAgIH0KCiAgICB2aXJ0dWFsIHZvaWQgcHJpbnQob3N0cmVhbSAmIG9zKSBjb25zdCB7CiAgICAgICAgb3MgPDwgIkRlcml2ZWQiOwogICAgfQoKICAgIGludCBtX2ludDsKfTsKCmNsYXNzIFN0b3JlIHsKcHVibGljOgoKICAgIFN0b3JlKGNvbnN0IHN0cmluZyYgcykgewogICAgICAgIG1fcyA9IHM7CiAgICB9CgogICAgdm9pZCBBZGQoY29uc3QgQmFzZSYgYikgewogICAgICAgIGNvdXQgPDwgYiA8PCBlbmRsOyAvL291dHB1dCBiZWZvcmUgc3RvcmluZyB0byB2ZWN0b3IKICAgICAgICB2LnB1c2hfYmFjaygmYik7CiAgICB9CgogICAgZnJpZW5kIG9zdHJlYW0gJiBvcGVyYXRvciA8PChvc3RyZWFtICYgb3MsIGNvbnN0IFN0b3JlICYgeCk7CgogICAgc3RyaW5nIG1fczsKICAgIHZlY3Rvcjxjb25zdCBCYXNlKj4gdjsKfTsKCm9zdHJlYW0gJiBvcGVyYXRvciA8PChvc3RyZWFtICYgb3MsIGNvbnN0IFN0b3JlICYgeCkgewogICAgZm9yIChhdXRvIGl0ID0geC52LmNiZWdpbigpOyBpdCAhPSB4LnYuY2VuZCgpOyArK2l0KSB7CiAgICAgICAgY291dCA8PCAqKCppdCkgPDwgZW5kbDsKICAgIH0KICAgIHJldHVybiBvczsKfQoKaW50IG1haW4oKSB7CiAgICBTdG9yZSBhKCJhYWFhYWFhIik7CiAgICBhLkFkZChEZXJpdmVkKCJhYmMiLCA1KSk7CiAgICBhLkFkZChEZXJpdmVkKCJkZWYiLCA2KSk7CgogICAgY291dCA8PCBlbmRsOwogICAgY291dCA8PCBhOyAvL291dHB1dCBhZnRlciBzdG9yaW5nIHRvIHZlY3RvcgoKICAgIHJldHVybiAwOwp9Cg==