How to create an object for a class that hasa constructor defined under privete access specifier

Showing Answers 1 - 42 of 42 Answers

antoni

  • Nov 13th, 2006
 

Use static function to create object.

  Was this answer useful?  Yes

ANonymous

  • Nov 15th, 2006
 

contructors are never private

  Was this answer useful?  Yes

Hemangi

  • Nov 17th, 2006
 

If the constructor/destructor is declared as private, then the class cannot be instantiated.

  Was this answer useful?  Yes

CHITTA RANJAN RAY

  • Nov 21st, 2006
 

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

  Was this answer useful?  Yes

Chitta Ranjan Ray(TCS-KOCHI)

  • Nov 21st, 2006
 

 Class having private constructor can create object for example singleton class have private consructor but creates single object. 

  Was this answer useful?  Yes

Chitta Ranjan Ray(TCS-KOCHI)

  • Nov 21st, 2006
 

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.

  Was this answer useful?  Yes

antariksh

  • Nov 27th, 2006
 

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;}

  Was this answer useful?  Yes

Anil

  • Nov 30th, 2006
 

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

object.

}

else

{

return ref;

}

  Was this answer useful?  Yes

Piyush

  • Dec 5th, 2006
 

Constructors can be private forsure, And there objects can be created by two ways:1) Using Static functions2) Using Friend functions

Vishrut Frank

  • Dec 7th, 2006
 

Use Static Method.. Refer singleton pattern!

  Was this answer useful?  Yes

richard lin

  • Dec 14th, 2006
 

Asssuming no friend functions (otherwise, I have to type more) use public static method to create it in the heap. Typical usage for  private constructor is to enforce the object creation from heap and prevent it from been inheriented.

  Was this answer useful?  Yes

Scott Smith

  • Jan 24th, 2007
 

Not true. Constructors can be private and often are for good reasons.

  Was this answer useful?  Yes

Prasad2008

  • Jun 10th, 2008
 

We have to define a public static member function for this purpose

class sample
{
sample* p;
sample();
public:
static sample genObject();
};
sample* sample::genObject()
{
p=new sample;
return p;
}
void main()
{
sample *p1=sample::genObject();

}



  Was this answer useful?  Yes

Prasad2008

  • Jun 10th, 2008
 

There was little error in the code in my  previous answer. We have to define a public static member function for this purpose. Here is the code

class sample
{
sample* p;
sample();
public:
static sample genObject();
};
sample* sample::genObject()
{
p=new sample;
return p;
}
void main()
{
sample *p1=sample::genObject();

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions