#include <iostream> // cout, endl
#include <memory> // shared_ptr
#include <string> // string
struct Base
{
virtual std::string GetName() const = 0;
~Base() { std::cout << "~Base\n"; }
};
struct Concrete : public Base
{
std::string GetName() const
{
return "Concrete";
}
~Concrete() { std::cout << "~Concrete\n"; }
};
int main()
{
{
std::cout << "test 1\n";
std::shared_ptr<Base> b(new Concrete);
std::cout << b->GetName() << std::endl;
}
{
std::cout << "test 2\n";
Base* pBase = new Concrete;
std::shared_ptr<Base> b(pBase);
std::cout << b->GetName() << std::endl;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPiAgIC8vIGNvdXQsIGVuZGwKI2luY2x1ZGUgPG1lbW9yeT4gICAgIC8vIHNoYXJlZF9wdHIKI2luY2x1ZGUgPHN0cmluZz4gICAgIC8vIHN0cmluZwoKc3RydWN0IEJhc2UKewogICB2aXJ0dWFsIHN0ZDo6c3RyaW5nIEdldE5hbWUoKSBjb25zdCA9IDA7CiAgIH5CYXNlKCkgeyBzdGQ6OmNvdXQgPDwgIn5CYXNlXG4iOyB9IAp9OwoKc3RydWN0IENvbmNyZXRlIDogcHVibGljIEJhc2UKewogICBzdGQ6OnN0cmluZyBHZXROYW1lKCkgY29uc3QKICAgewogICAgICByZXR1cm4gIkNvbmNyZXRlIjsKICAgfQogICB+Q29uY3JldGUoKSB7IHN0ZDo6Y291dCA8PCAifkNvbmNyZXRlXG4iOyB9IAp9OwoKaW50IG1haW4oKQp7CiAgewogICAgc3RkOjpjb3V0IDw8ICJ0ZXN0IDFcbiI7CiAgICBzdGQ6OnNoYXJlZF9wdHI8QmFzZT4gYihuZXcgQ29uY3JldGUpOwogICAgc3RkOjpjb3V0IDw8IGItPkdldE5hbWUoKSA8PCBzdGQ6OmVuZGw7CiAgfQogICAKICB7CiAgICBzdGQ6OmNvdXQgPDwgInRlc3QgMlxuIjsKICAgIEJhc2UqIHBCYXNlID0gbmV3IENvbmNyZXRlOwogICAgc3RkOjpzaGFyZWRfcHRyPEJhc2U+IGIocEJhc2UpOwogICAgc3RkOjpjb3V0IDw8IGItPkdldE5hbWUoKSA8PCBzdGQ6OmVuZGw7CiAgfQogIAp9