0

I understand what explicit keyword does to a constructor with a single parameter, it can surely limit the possibility of unwanted behaviour. I also understand what move and copy constructors do.

What I don't understand is, how explicit constructor affects move and copy constructors, and I have also seen a case when you can disable copy and move constructors... Why would you do all that? All that is very vague to me.

  • Possible duplicate of http://stackoverflow.com/questions/25529772/explicit-copy-constructor-and-stdsort?rq=1 ? – Rene Dec 08 '16 at 14:16
  • You'd disable copying because it may not be logical to copy objects of a class. Singleton's shouldn't be copyable, for instance. – StoryTeller - Unslander Monica Dec 08 '16 at 14:21
  • I am asking about explicit copy constructor, but about explicit constructor, and how it affects copy and move constructors. That does not answer my question, but thanks, I will read that too. – Damir Bajramovic Dec 08 '16 at 14:25

1 Answers1

0

An explicit copy constructor works the same way as an explicit converting constructor - it cannot be invoked implicitly. The only difference is what your object is constructed from.

I would also believe that the uses cases for this are extremely rare and far between.

Slightly more common case is to have the class' copy constructor and assignment operator deleted (= delete). This is useful for objects that represent a unique resource, and therefore should not be copied. std::cout is an example of an object that exists, but should not be copied.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203