#include <iostream>
#include <memory>
namespace myns
{
class Base
{
public:
Base() = default;
virtual ~Base() = default;
virtual void Create()
{
std::cout << "Created in Base" << std::endl;
}
virtual void Destroy()
{
std::cout << "Destroyed in Base - " << std::endl;
}
};// class Base
class ADerivded : public Base
{
public:
void Create() override
{
std::cout << "Created in ADerivded" << std::endl;
}
void Destroy() override
{
std::cout << "Destroyed in ADerivded" << std::endl;
}
};// class ADerivded
template<typename T>
void unmake(T* ptr)
{
ptr->Destroy();
delete ptr;
}
template<typename T, typename... Arguments>
std::shared_ptr<T> make(Arguments&&... args)
{
std::shared_ptr<T> ptr = std::shared_ptr<T>(new T(), myns::unmake<T>);
ptr->Create(std::forward<Arguments>(args)...);
return ptr;
}
}//namespace myns
using namespace std;
int main()
{
shared_ptr<myns::ADerivded> a = myns::make<myns::ADerivded>();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5PgoKCm5hbWVzcGFjZSBteW5zCnsKCmNsYXNzIEJhc2UKewpwdWJsaWM6CgogICAgQmFzZSgpID0gZGVmYXVsdDsKICAgIHZpcnR1YWwgfkJhc2UoKSA9IGRlZmF1bHQ7CgogICAgdmlydHVhbCB2b2lkIENyZWF0ZSgpCiAgICB7CiAgICAgICAgc3RkOjpjb3V0ICA8PCAiQ3JlYXRlZCBpbiBCYXNlIiA8PCBzdGQ6OmVuZGw7CiAgICB9CgogICAgdmlydHVhbCB2b2lkIERlc3Ryb3koKQogICAgewogICAgICAgIHN0ZDo6Y291dCAgPDwgIkRlc3Ryb3llZCBpbiBCYXNlIC0gIiA8PCBzdGQ6OmVuZGw7CiAgICB9Cn07Ly8gY2xhc3MgQmFzZQoKY2xhc3MgQURlcml2ZGVkIDogcHVibGljIEJhc2UKewpwdWJsaWM6CgogICAgdm9pZCBDcmVhdGUoKSBvdmVycmlkZQogICAgewogICAgICAgIHN0ZDo6Y291dCAgPDwgIkNyZWF0ZWQgaW4gQURlcml2ZGVkIiA8PCBzdGQ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