fork download
  1. #include <iostream>
  2.  
  3. class Hoge{
  4. Hoge(){std::cout<<"new"<<std::endl;}
  5. Hoge(Hoge const &);
  6. Hoge& operator=(Hoge const &);
  7. ~Hoge(){std::cout<<"del"<<std::endl;}
  8. public:
  9. static Hoge* Create(){return new Hoge();}
  10. static void Delete(Hoge* p){delete p;}
  11. void f(){std::cout<<"f"<<std::endl;}
  12. void f()const{std::cout<<"f const"<<std::endl;}
  13. };
  14.  
  15. class HogePtr{
  16. Hoge* p;
  17. HogePtr(const HogePtr&);
  18. HogePtr& operator=(const HogePtr&);
  19. public:
  20. HogePtr(Hoge* ap):p(ap){}
  21. ~HogePtr(){Hoge::Delete(p);}
  22. Hoge& operator*(){return *p;}
  23. const Hoge& operator*() const {return *p;}
  24. Hoge* operator->(){return p;}
  25. const Hoge* operator->() const{return p;}
  26. };
  27.  
  28. int main(){
  29. HogePtr p(Hoge::Create());
  30. p->f();
  31.  
  32. const HogePtr p2(Hoge::Create());
  33. p2->f();
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0.02s 2812KB
stdin
Standard input is empty
stdout
new
f
new
f const
del
del