Defining an object without calling its constructor in C++

Store a pointer to an Object rather than an actual Object


class Program
   Object* myObject; // Will not try to call the constructor or do any initializing
      //Do initialization
      myObject = new Object(...);  // Initialised now


Don’t forget to delete it in the destructor. Modern C++ helps you there, in that you could use an auto_ptr shared_ptr rather than a raw memory pointer.

Leave a Comment