#include <iostream>
#include <memory>
using namespace std;
//////////////////////////////////////////////////////////////
struct Abstract
{
virtual void Release() = 0;
virtual void DoSomething() = 0;
};
extern "C" Abstract* NewAbstract();
//////////////////////////////////////////////////////////////
class AbstractImpl : public Abstract
{
public:
AbstractImpl();
~AbstractImpl();
void Release();
void DoSomething();
};
Abstract* NewAbstract()
{
cout << __PRETTY_FUNCTION__ << endl;
return new (nothrow) AbstractImpl();
}
AbstractImpl::AbstractImpl()
{
cout << __PRETTY_FUNCTION__ << endl;
}
AbstractImpl::~AbstractImpl()
{
cout << __PRETTY_FUNCTION__ << endl;
}
void AbstractImpl::Release()
{
cout << __PRETTY_FUNCTION__ << endl;
delete this;
}
void AbstractImpl::DoSomething()
{
cout << __PRETTY_FUNCTION__ << endl;
}
//////////////////////////////////////////////////////////////
int main()
{
auto p = NewAbstract();
if (!p)
{
return -1;
}
shared_ptr<Abstract> sp(p, mem_fn(&Abstract::Release));
sp->DoSomething();
return 0;
}