GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 105 of 203    Print  
how to create an object for a class that has
a constructor defined under privete access specifier

  
Total Answers and Comments: 14 Last Update: June 10, 2008     Asked by: elizabeth 
  
 Sponsored Links

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

Above answer was rated as good by the following members:
yzesong
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
November 13, 2006 04:34:49   #1  
antoni        

RE: how to create an object for a class that has...
Use static function to create object.
 
Is this answer useful? Yes | No
November 15, 2006 06:14:18   #2  
ANonymous        

RE: how to create an object for a class that has...
contructors are never private
 
Is this answer useful? Yes | No
November 17, 2006 01:39:14   #3  
Hemangi        

RE: how to create an object for a class that has...
If the constructor/destructor is declared as private then the class cannot be instantiated.

 
Is this answer useful? Yes | No
November 21, 2006 06:26:31   #4  
CHITTA RANJAN RAY        

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
 
Is this answer useful? Yes | No
November 21, 2006 06:32:16   #5  
Chitta Ranjan Ray(TCS-KOCHI)        

RE: how to create an object for a class that has...
Class having private constructor can create object for example singleton class have private consructor but creates single object.
 
Is this answer useful? Yes | No
November 21, 2006 06:39:53   #6  
Chitta Ranjan Ray(TCS-KOCHI)        

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.
 
Is this answer useful? Yes | No
November 27, 2006 06:40:02   #7  
antariksh        

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;}
 
Is this answer useful? Yes | No
November 30, 2006 05:23:47   #8  
Anil        

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

object.

}

else

{

return ref;

}


 
Is this answer useful? Yes | No
December 05, 2006 20:50:15   #9  
Piyush        

RE: how to create an object for a class that has...
Constructors can be private forsure And there objects can be created by two ways:1) Using Static functions2) Using Friend functions
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
December 07, 2006 11:08:59   #10  
Vishrut Frank        

RE: how to create an object for a class that has...

Use Static Method.. Refer singleton pattern!


 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape