Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Section A.2. Deleted functions

A.2. Deleted functions

Sometimes it doesn’t make sense to allow a class to be copied. std::mutex is a prime example of this—what would it mean if you did copy a mutex? std::unique_lock<> is another—an instance is the one and only owner of the lock it holds. To truly copy it would mean that the copy also held the lock, which doesn’t make sense. Moving ownership between instances, as described in section A.1.2, makes sense, but that’s not copying. I’m sure you’ve met other examples.

The standard idiom for preventing copies of a class used to be to declare the copy constructor and copy assignment operator private and then not provide an implementation. This would cause a compile error if any code outside the class in question tried to copy an instance and a link-time error (due to lack of an implementation) if any of the class’s member functions or friends tried to copy an instance:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial