Effective C + + learning NOTE Clause 05: Understand what functions C + + silently writes and calls

Source: Internet
Author: User

If the user does not provide constructors, copy constructors, copy assignment operators, and destructors, the compiler will help you create them only if they are needed. These functions generated by the compiler are public and inline.

When you write down a class like this:

class Empty {};

If the above function is required, then the compilation will automatically help you to change the class to the following:

 class   empty{ public      //  here is the default initialization of the data member, if there is a virtual function, This also configures the virtual function table  Empty (const  empty& RHS)    {...}  ~empty () {..} //     empty& operator  = (const  empty&< Span style= "color: #000000;" > RHS) {...}};  

  Subject to detailed textual research in inside C + + object model (Note: In fact, for the copy constructor, assignment operator overloading, not you did not write yourself, when the need to be called when it was generated!) If the compiler determines that the required copy constructor for this class is trival, the trivial copy constructor, then the compiler will not generate a copy ' constructor, etc. , but only where it needs the copy constructor or assignment operator overloading. The execution of a bit-wise copy).

Second, note that the destructor of the compiler output is a non-vitual, unless the class's base class itself declares a virtual destructor.

Note that in a class containing reference members, the assignment operation is supported and you must define the copy assignment operator yourself. The same is true for the class with the const member included.

Otherwise the compiler will prompt you:

non-Static reference member ' std::stringdefaultoperator

Or

non-staticconst member 'conststringdefaultoperator

Iv. if a base class declares the copy assignment operator as private, the compiler will refuse to generate a copy assignment operator for its derived class.

Effective C + + learning NOTE Clause 05: Understand what functions C + + silently writes and calls

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.