Guest User

Untitled

a guest
Jan 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. class Base
  2. {
  3. protected:
  4. Base()
  5. {}
  6.  
  7. public:
  8. virtual void initialize()
  9. {}
  10.  
  11. template<typename D, typename... Ts>
  12. static std::shared_ptr<D> create(Ts... args)
  13. {
  14. auto d = std::shared_ptr<D>(new D(args...));
  15. d->initialize();
  16. return d;
  17. }
  18. };
  19.  
  20. class Derived : public Base
  21. {
  22. private:
  23. int value_;
  24.  
  25. protected:
  26.  
  27. public:
  28.  
  29. Derived(int v) : value_(v)
  30. {}
  31.  
  32. void initialize() override
  33. {
  34. //Do something with value_
  35. }
  36. };
  37.  
  38.  
  39. int main()
  40. {
  41. auto derived = Base::create<Derived>(42);
  42. // etc.
  43.  
  44. return 0;
  45. }
Add Comment
Please, Sign In to add comment