Effective C + + learning NOTE Clause 06: If you do not want to use the compiler automatically generated functions, you should explicitly deny

Source: Internet
Author: User

First, in order to dismiss the function provided automatically by the compiler, the corresponding member function can be declared private and not implemented . (the compiler will help you if you just don't implement it yourself)

Such as:

 class   a{ public  : A ( const  string  & name): M_name (name) {}  private  :  //  rejects copy and assignment, declares private, and declares only that it does not implement  A (const  a&); A  & operator  = (const  a&< Span style= "color: #000000;"    >);  private  :  string   M_name;};  

Second, for the class that denies the assignment copy, you can also inherit a uncopyable base class .

classuncopyable{ Public: Uncopyable () {}~uncopyable () {}Private: Uncopyable (Constuncopyable&); Uncopyable&operator=(Constuncopyable&);};
classA: PublicUncopyable//Inherit uncopyable not necessarily public!{ Public: A (Const string&name): M_name (name) {}Private: stringm_name;};

Effective C + + learning NOTE Clause 06: If you do not want to use the compiler automatically generated functions, you should explicitly deny

Related Article

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.