How C + + prohibits copying operations for objects

Source: Internet
Author: User

The easiest thing to think about is declaring the copy constructor and the assignment function as private. However, private only says that the external cannot be called directly, but it can be accessed indirectly through the class's member function and the friend function. So what do we do?

----"In a class, it is permissible to declare a function, but it is not possible to implement the function, which is legal. So even if the function is declared in public, but not implemented, then calling this function will also make an error.


Well, we can use the feature together,boost::noncopyable .

[CPP]View PlainCopy
  1. #ifndef boost_noncopyable_hpp_included
  2. #define Boost_noncopyable_hpp_included
  3. Namespace Boost {
  4. Private copy constructor and copy assignment ensure classes derived from
  5. Class Noncopyable cannot be copied.
  6. Contributed by Dave Abrahams
  7. namespace Noncopyable_ //protection from unintended ADL
  8. {
  9. class Noncopyable
  10. {
  11. protected:
  12. Noncopyable () {}
  13. ~noncopyable () {}
  14. private: //Emphasize the following members is private
  15. Noncopyable ( const noncopyable&);
  16. Const noncopyable& operator= ( const noncopyable&);
  17. };
  18. }
  19. typedef noncopyable_::noncopyable Noncopyable;
  20. } //namespace Boost
  21. #endif//boost_noncopyable_hpp_included



In order to prohibit copying of objects, we only need to let their private inherit from Boost::noncopyable,

Class student:private boost::noncopyable

{

......

}

When copying a copy constructor or assignment function called to a derived class, it is unavoidable to call the corresponding function of the base class, because these operations are private, and such operations are rejected by the compiler.

It is important to note that multiple inheritance can sometimes invalidate an empty base class noncopyable optimization, so this is not suitable for multiple inheritance scenarios.



In addition, if you just don't want to use the default copy constructor or assignment function, you can use the delete provided by C++11,

Class MyClass
{
Public
MyClass () =default;
MyClass (const myclass&) =delete;
......
}

Of course, once the function has been deleted, overloading the function is also illegal, which we are accustomed to call a delete function.

How C + + prohibits copying operations for objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.