constructor

N

nik

hi friends,
plz tell me if we declare constructor as private member of class and
want to create obj of that class. Is it possible? the way in which the
situation is solved
 
O

osmium

nik said:
plz tell me if we declare constructor as private member of class and
want to create obj of that class. Is it possible? the way in which the
situation is solved

Look for the various methods of producing singletons.
 
M

mlimber

nik said:
plz tell me if we declare constructor as private member of class and
want to create obj of that class. Is it possible? the way in which the
situation is solved

There are several ways. You might have a friend who could create an
instance because it has access to the private parts, or there might be
a public function that creates instance(s) of the class:

class A
{
private:
A() {}
public:
static std::auto_ptr<A> Create()
{
return std::auto_ptr<A>( new A );
}
};

The constructor might also be private if you shouldn't use that
constructor or create instances of the class at all, e.g., if it's a
template typedef equivalent:

template<class T, class U>
struct B
{
typedef T Type1;
typedef U Type2;
};

template<class U>
struct BWithInt
{
typedef B<int,U> Type;
private:
BWithInt(); // Private and undefined on purpose
};

BWithInt<float> someB; // Compile-time error: private c-tor
BWithInt<float>::type someOtherB; // Ok

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top