P
Preets
Can anyone explain to me the exact use of private constructors in c++ ?
For example for implement singleton patternCan anyone explain to me the exact use of private constructors in c++ ?
Preets said:Can anyone explain to me the exact use of private constructors in c++ ?
Preets said:can a class derive from singleton class ? And, can any number of
instances of that derived class be created ?
If you write a singleton class in C++. you will need to play some
tricks make all constructors private, including default, copy and
assignment operators.
Preets said:Can anyone explain to me the exact use of private constructors in c++ ?
Hi,
Could you explain the reasons why would somebody write a singleton class in
C++ instead of using the good old C-style singleton pattern?
The advantages of the C-style singleton (file-local data with public
interface) are better information hiding, and built in compiler firewall.
But what are the advantages of the singleton class?
godescbach said:One reason you might want to use a singleton is to inherit behaviour
from another class.
Perhaps you want to group a one or more objects into one singleton to
ensure only one instance for a particular program. The classes that
define these objects are not singletons because they can have more
that one instance in other programs.
IMO, it is best to group data and the functions that operate on it in
a class. This takes information hiding a step further by putting the
data and its functionality into class. All access must go through the
class. Not sure what you mean by 'compiler firewall' or how using the
C-style has any advantage over a class singleton in terms of the
compiler.
Clients of a singleton class don't really know or care if the class is
a singleton. It does not impact the interface.
Using the C-style
singleton your public interface doesn't need a reference to the object
passed in because you interface knows there's only one instance with
which to work.
So someone looking at youi code can tell if a group of
functions operating on some data are operating on only one instance of
that data. The class singleton is better at hiding this fact. But
this is rather subjective in any case.
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.