Skip to main content
added 76 characters in body
Source Link
mathreadler
  • 222
  • 2
  • 11

Thank you very much for feedback, everyone.

Based on answers I came up with a minimal example that works for me.

I suppose we can call it a kind of combination of multi-stage construction and Factory.

template<class C, class Cargs> class ArgFactory{ // Here is factory. The class C shall have C(Cargs a)  // and define a doAllocate(Cargs a) public: static C* Create(Cargs a){ C* lRet = new C(a); lRet->doAllocate(a); // This is now run after actual construction. return lRet; } virtual void doAllocate(Cargs a) = 0; // This overrideable. }; class IntegerArgs{ public: IntegerArgs(int a) : m(a){} int m; }; class Integer : public ArgFactory<Integer,IntegerArgs>{ public: Integer(IntegerArgs a){} virtual void doAllocate(IntegerArgs a){ m=a.m; } int m; }; 

Thank you very much for feedback, everyone.

Based on answers I came up with a minimal example that works for me.

I suppose we can call it a kind of combination of multi-stage construction and Factory.

template<class C, class Cargs> class ArgFactory{ // Here is factory. The class C shall have C(Cargs a) public: static C* Create(Cargs a){ C* lRet = new C(a); lRet->doAllocate(a); // This is now run after actual construction. return lRet; } virtual void doAllocate(Cargs a) = 0; // This overrideable. }; class IntegerArgs{ public: IntegerArgs(int a) : m(a){} int m; }; class Integer : public ArgFactory<Integer,IntegerArgs>{ public: Integer(IntegerArgs a){} virtual void doAllocate(IntegerArgs a){ m=a.m; } int m; }; 

Thank you very much for feedback, everyone.

Based on answers I came up with a minimal example that works for me.

I suppose we can call it a kind of combination of multi-stage construction and Factory.

template<class C, class Cargs> class ArgFactory{ // Here is factory. The class C shall have C(Cargs a)  // and define a doAllocate(Cargs a) public: static C* Create(Cargs a){ C* lRet = new C(a); lRet->doAllocate(a); // This is now run after actual construction. return lRet; } virtual void doAllocate(Cargs a) = 0; // This overrideable. }; class IntegerArgs{ public: IntegerArgs(int a) : m(a){} int m; }; class Integer : public ArgFactory<Integer,IntegerArgs>{ public: Integer(IntegerArgs a){} virtual void doAllocate(IntegerArgs a){ m=a.m; } int m; }; 
Source Link
mathreadler
  • 222
  • 2
  • 11

Thank you very much for feedback, everyone.

Based on answers I came up with a minimal example that works for me.

I suppose we can call it a kind of combination of multi-stage construction and Factory.

template<class C, class Cargs> class ArgFactory{ // Here is factory. The class C shall have C(Cargs a) public: static C* Create(Cargs a){ C* lRet = new C(a); lRet->doAllocate(a); // This is now run after actual construction. return lRet; } virtual void doAllocate(Cargs a) = 0; // This overrideable. }; class IntegerArgs{ public: IntegerArgs(int a) : m(a){} int m; }; class Integer : public ArgFactory<Integer,IntegerArgs>{ public: Integer(IntegerArgs a){} virtual void doAllocate(IntegerArgs a){ m=a.m; } int m; };