RE: how to create an object for a class that has...
we can a create a single object even if the constructor is private.Example of of it is sigleton class having only one object even if its constructor is private.But make sure that class should consists atleast one static variable is must
RE: how to create an object for a class that has...
Class having private constructor can also create object.Exampe of it is singlton class but make sure that there should be a static variable in the class.
RE: how to create an object for a class that has...
yeah we can create object of a class having private constructor. it only requires that there must be static function which can create an object.But Chitta i dont understand why there is necessity of a static variable in class?code for class---class CMyClass{private:CMyClass(){//write some code} public: static CMyClass* GetPointerToMe(){ //u can also overload this function to suite ur own constructors return new CMyClass; }};int main(void){ CMyClass* cmc CMyClass::GetPointerToMe();return 0;}
RE: how to create an object for a class that has...
Hello antariksh There is need for static reference of the same class. Since if you call that getPointer* function 100 times it wil consturct the object 100 times (i.e. 100 new objects). So if you maintain a static reference of that class say static Myclass ref you can code like this.
if (ref ! null)
{
call the private constructor and return the constructed