cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Assignment prevention?
Assignment prevention?
Mar 9, 2010 at 8:30am UTC
Duy3
(3)
I noticed people put both operator= and dtor member functions in class private section to prevent copy at assignment.
For example:
http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B
Could someone think of a scenario where assignment prevention is necessary (the operator= function). In my opinion, for singleton case, it is not; because hidding the dtor wont allow any "foreign" instances. No?
Mar 9, 2010 at 8:58am UTC
kbw
(9488)
It doesn't make sense to copy some objects, like a factory class for example. In general manager classes tend not to be 'copyable'.
Mar 9, 2010 at 1:17pm UTC
jsmith
(5804)
But hiding the destructor means you can't destroy
any
instance of the class, even ones that
aren't copies.
The non-copyable pattern is to declare the copy constructor and assignment operator private
and leave them unimplemented.
Assignment and copy are very, very similar.
Typically
if you don't want copyability then
you don't want assignability either, and vice versa.
Topic archived. No new replies allowed.