fork download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. //////////////////////////////////////////////////////////////
  6. struct Abstract
  7. {
  8. virtual void Release() = 0;
  9.  
  10. virtual void DoSomething() = 0;
  11. };
  12.  
  13. extern "C" Abstract* NewAbstract();
  14.  
  15. //////////////////////////////////////////////////////////////
  16. class AbstractImpl : public Abstract
  17. {
  18. public:
  19. AbstractImpl();
  20.  
  21. ~AbstractImpl();
  22.  
  23. void Release();
  24.  
  25. void DoSomething();
  26. };
  27.  
  28. Abstract* NewAbstract()
  29. {
  30. cout << __PRETTY_FUNCTION__ << endl;
  31. return new (nothrow) AbstractImpl();
  32. }
  33.  
  34. AbstractImpl::AbstractImpl()
  35. {
  36. cout << __PRETTY_FUNCTION__ << endl;
  37. }
  38.  
  39. AbstractImpl::~AbstractImpl()
  40. {
  41. cout << __PRETTY_FUNCTION__ << endl;
  42. }
  43.  
  44. void AbstractImpl::Release()
  45. {
  46. cout << __PRETTY_FUNCTION__ << endl;
  47. delete this;
  48. }
  49.  
  50. void AbstractImpl::DoSomething()
  51. {
  52. cout << __PRETTY_FUNCTION__ << endl;
  53. }
  54.  
  55. //////////////////////////////////////////////////////////////
  56. int main()
  57. {
  58. auto p = NewAbstract();
  59.  
  60. if (!p)
  61. {
  62. return -1;
  63. }
  64.  
  65. shared_ptr<Abstract> sp(p, mem_fn(&Abstract::Release));
  66.  
  67. sp->DoSomething();
  68.  
  69. return 0;
  70. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Abstract* NewAbstract()
AbstractImpl::AbstractImpl()
virtual void AbstractImpl::DoSomething()
virtual void AbstractImpl::Release()
AbstractImpl::~AbstractImpl()