Instead of the singleton design pattern, if the above function is
used, what are the drawbacks of using the above function ? What are
the advantages of singleton design pattern over the above function ?
The constructor of Test must be public for the above to work. So the
compiler would not reject, when a second instance of Test is created. It
is only up to the programmer whether to use Test as a singleton or not.
Of course alternatively you could make getTestInstance a friend of Test.
But making it a static member of Test is more clear.
Furthermore if the application should provide a defined behaviour at
shutdown, you must delete Test somewhere.
static boost::scoped_ptr<Test> instance;
will do this job for you. If you do not have boost, std::auto_ptr is
fine too.
In that case, there is no point (no pun intended) in using a pointer. You
can simply create an instance directly. However, both have another issue.