Hi all,

I am trying to understand the concept of private constructors and singleton.
As far as i have studied, we can have private constructors , destructors and that they are used to avoid making instances of the class other than its member function.
now that i am trying to implement it i am facing few problems
how can i have an object of a class whose constructor is private?
also how will i call a member function in main( if we use static please explain how)
Can we please discuss on this so that my fundamentals are clear?

You can provide static create() member functions which create the object using new and return a pointer to the allocated object.

typedef  auto_ptr<Test>  TestPtr;
TestPtr Test::create() throw(bad_alloc)
{ 
return new Test();
}

There are still some doubts

I am still not having very clear idae about it.
its fine we have a member function create of class Test, but what next? how are we going to call this function in main() without creating any object of class Test.
Also what is the significance of auto_ptr?
please explain

I am still not having very clear idae about it.
its fine we have a member function create of class Test, but what next? how are we going to call this function in main() without creating any object of class Test.

Yes, you can do this in main TestPtr p(Test::create());

Also what is the significance of auto_ptr?
please explain

auto_ptr is a pointer-like object (a smart pointer) whose destructor automatically calls delete on what it points to as soon as scope ends i.e at }

struct singleton
{
  // return a reference, not a pointer. the problem with returning
  // a pointer is that a caller might be tempted to call delete on it
  // returning an auto_ptr<> to a singleton is ruled out; 
  // it's (or it's copy's) destructor *will* call delete on the object.
  static singleton& get_the_only_object() ;

  /* other operations */
           
  private :
    
    // no accessible constructor; only way to get the singleton
    // is through the public *static* method 
    singleton() { /* ... */ }
    
    // copy constructor; there is only one object, copy of it should not
    // be created. do not define this!
    singleton( const singleton& ) ;

    // only one object; assignment is always a trivial self-assignment
    // do not define this!
    void operator= ( const singleton& ) ;

    /* other private stuff */
};

// this simple idea was first presented by scott myers
// see 'more effective c++', item 26
singleton& singleton::get_the_only_object()
{
  // created the first time flow of control reaches this point
  static singleton the_only_one ;
  // destroyed after main returns 
  return the_only_one ;
}

int main()
{
  // a user gets (a reference to) the singleton object this way
  singleton& object = singleton::get_the_only_object() ;
  // object.do_something() ;
}

this works fine in many cases. however, as andrei alexandrescu points out in his book 'modern c++ design', "The singleton design pattern is unique in that ... it's description is simple, but it's implementation is complicated". and then goes on to discuss singleton implementation issues in some 25 pages or so.

Yes, you can do this in main TestPtr p(Test::create()); auto_ptr is a pointer-like object (a smart pointer) whose destructor automatically calls delete on what it points to as soon as scope ends i.e at }

Thanks:)

struct singleton
{
  // return a reference, not a pointer. the problem with returning
  // a pointer is that a caller might be tempted to call delete on it
  // returning an auto_ptr<> to a singleton is ruled out; 
  // it's (or it's copy's) destructor *will* call delete on the object.
  static singleton& get_the_only_object() ;
 
  /* other operations */
 
  private :
 
    // no accessible constructor; only way to get the singleton
    // is through the public *static* method 
    singleton() { /* ... */ }
 
    // copy constructor; there is only one object, copy of it should not
    // be created. do not define this!
    singleton( const singleton& ) ;
 
    // only one object; assignment is always a trivial self-assignment
    // do not define this!
    void operator= ( const singleton& ) ;
 
    /* other private stuff */
};
 
// this simple idea was first presented by scott myers
// see 'more effective c++', item 26
singleton& singleton::get_the_only_object()
{
  // created the first time flow of control reaches this point
  static singleton the_only_one ;
  // destroyed after main returns 
  return the_only_one ;
}
 
int main()
{
  // a user gets (a reference to) the singleton object this way
  singleton& object = singleton::get_the_only_object() ;
  // object.do_something() ;
}

this works fine in many cases. however, as andrei alexandrescu points out in his book 'modern c++ design', "The singleton design pattern is unique in that ... it's description is simple, but it's implementation is complicated". and then goes on to discuss singleton implementation issues in some 25 pages or so.

Thanks :)

Another question...what is the basic purpose of having a private constructor or destructor?
A destructor is called when the object goes out of scope and by default it is public.What if we have a private destructor.What is its use?

the simple answer is that the implementor does not want anyone else to be able to destroy the object. examples are: a. a singleton object which should be destroyed only when the program ends b. the object is reference counted and will self-destruct when all outstanding references have been released. such objects cannot have an automatic storage duration (storage duration would be dynamic); so destruction at end of scope would not apply to these objects. see: http://www.boost.org/libs/smart_ptr/sp_techniques.html#preventing_delete

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.