#include <iostream>
#include <vector>
class Base
{
public:
virtual void show_data() {
std::cout<<__PRETTY_FUNCTION__<<std::endl;
}
virtual ~Base(){}
};
class Derived: public Base
{
public:
virtual void show_data() override {
std::cout<<__PRETTY_FUNCTION__<<std::endl;
std::cout<<"Just derived"<<std::endl;
std::cout<<"Such useful"<<std::endl;
}
};
class Special: public Base
{
public:
Special(int num, const std::string &data): secret_num(num), secret_data(data)
{}
virtual void show_data() override {
std::cout<<__PRETTY_FUNCTION__<<std::endl;
std::cout<<"Num: "<<secret_num<<std::endl;
std::cout<<"Data: "<<secret_data<<std::endl;
}
void set_params(int num, const std::string &data){
secret_num = num;
secret_data = data;
}
private:
int secret_num;
std::string secret_data;
};
int main()
{
std::vector<Base *> objects{
new Base(),
new Special(1111, "mheh"),
new Derived()
};
for (const auto &it: objects)
{
Special *special = dynamic_cast<Special *>(it);
if (special != nullptr)
{
special->show_data();
special->set_params(0, "new data");
special->show_data();
}
else
{
it->show_data();
}
std::cout<<"========="<<std::endl;
}
for (auto &it: objects){
delete it;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKY2xhc3MgQmFzZQp7CnB1YmxpYzoKICAgIHZpcnR1YWwgdm9pZCBzaG93X2RhdGEoKSB7CiAgICAgICAgc3RkOjpjb3V0PDxfX1BSRVRUWV9GVU5DVElPTl9fPDxzdGQ6OmVuZGw7CiAgICB9CiAgICB2aXJ0dWFsIH5CYXNlKCl7fQp9OwoKY2xhc3MgRGVyaXZlZDogcHVibGljIEJhc2UKewpwdWJsaWM6CiAgICB2aXJ0dWFsIHZvaWQgc2hvd19kYXRhKCkgb3ZlcnJpZGUgewogICAgICAgIHN0ZDo6Y291dDw8X19QUkVUVFlfRlVOQ1RJT05fXzw8c3RkOjplbmRsOwogICAgICAgIHN0ZDo6Y291dDw8Ikp1c3QgZGVyaXZlZCI8PHN0ZDo6ZW5kbDsKICAgICAgICBzdGQ6OmNvdXQ8PCJTdWNoIHVzZW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ihpdCk7CiAgICAgICAgaWYgKHNwZWNpYWwgIT0gbnVsbHB0cikKICAgICAgICB7CiAgICAgICAgICAgIHNwZWNpYWwtPnNob3dfZGF0YSgpOwogICAgICAgICAgICBzcGVjaWFsLT5zZXRfcGFyYW1zKDAsICJuZXcgZGF0YSIpOwogICAgICAgICAgICBzcGVjaWFsLT5zaG93X2RhdGEoKTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgaXQtPnNob3dfZGF0YSgpOwogICAgICAgIH0KICAgICAgICBzdGQ6OmNvdXQ8PCI9PT09PT09PT0iPDxzdGQ6OmVuZGw7CiAgICB9CiAgICAKICAgIGZvciAoYXV0byAmaXQ6IG9iamVjdHMpewogICAgICAgIGRlbGV0ZSBpdDsKICAgIH0KfQ==