How to prohibit C + + class support copy

Source: Internet
Author: User

How to prohibit C + + class support copy
The C + + compiler silently does a lot of work for you

    • When you write down
      Class Empty {};//is actually equivalent to class empty {public:    empty () {...}//default constructor    empty (const empty &RHS) {.. . }//copy Constructor    ~empty () {...}//destructor empty& operator=    (const Empty &RHS) {...}//copy Assig Nment};
    • However, there are times when you want to prohibit a class from replicating.

      Suppose you do not declare a copy constructor or copy assignment operator, the compiler will generate a copy for you; Suppose you declare them. Your class supports the same copying.

How to resolve:

  • the copy constructor and copy assignment are declared as private, and do not give the implementation; in general, member functions and friend functions are likely to call them, which can result in a link error (because you do not define a function)
    Class A {public:private:    A (const a &RHS);//Only Declarations    a& operator= (const a &RHS);};
  • a better approach. The ability to transfer connection errors of procedure 1 to the compile time (the sooner you find the better the error). This is possible because the class inherits the base class such as the following : only if anyone----or even a member function or a friend function-----try to copy the object, the compiler will try to generate a copy constructor and copy assignment operators. The compiler-generated version of these functions attempts to invoke the corresponding sibling of the base class, and these calls are rejected by the compiler, so the copy function of the base class is private.
    Class Uncopyable {public:  uncopyable () {}  ~uncopyable () {}private:  uncopyable (const uncopyable&);  uncopyable& operator= (const uncopyable&);}; Class B:private uncopyable {};
    Another point to note is that inheritance is private rather than public. This prevents public inheritance from being inherited. The following code: (Memory leak, the base class must be virtual virtual function, or memory leak, because the derived class object is deleted through a pointer to the base class)
    Uncopyable *p = new B ();.. delete P;



How to disable C + + class support copy

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.