c++ - Return polymorphic type without dynamic memory allocation? -


is there standard design pattern can return polymorphic type procedure without being required dynamically allocate memory object within procedure?

or polymorphism , dynamic memory allocation go hand-in-hand in practice?

i'm hoping c++03-compatible solution, if there c++11 1 i'm interested in seeing well.

object pool

really performance, reduce allocations; seems applicable in scenario however, assuming have limit on number of instances of object. code below illustrates idea. requires priori knowledge of types involved (as need allocate necessary object buffers). deleting (returning type pool) requires care , knowledge of type. can done via virtual function call.

template< typename type > struct typepool {     static typepool& getinstance(){ return pool; }      type& getpooledobject()      {         return arr[0]; // real tracking here     }    private:     static typepool< type > pool;     type arr[10]; };  struct a{ }; struct b : public { }; struct c : public { };  // catch design... typepool<a> typepool<a>::pool; typepool<b> typepool<b>::pool; typepool<c> typepool<c>::pool;  struct objectfactory {     // actual function call construct object     // objectfactory::gettype< > or gettype< b >     template< typename type >     static a& gettype()     {         return typepool<a>::getinstance().getpooledobject();     } };   int main( int argc, char* r[] ) {     a& object = objectfactory::gettype< c >(); } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -