What functions the C + + compiler silently writes and calls

Source: Internet
Author: User

When is empty class no longer a null? When C + + has processed it, yes, if you don't declare it yourself, the compiler will declare it (compiler Version) a copy constructor, a copy assignment operator, and a destructor. In addition, if you do not declare any constructors, the compiler will also declare a default constructor for you. All of these are public and inline.

The copy assignment operator generated by the compiler behaves essentially like the copy constructor, but is generally only generated if the resulting code is legitimate and has the appropriate opportunity to prove it makes sense.

If a reference data member and a const member exist in the class, copy assignment is not generated at this time. Because C + + does not allow "to change a reference to a different object".

In the face of such a problem, C + + response is to refuse to compile the line assignment action. If you intend to support assignment operations within a class that contains reference members, you must define the copy assignment operator yourself. The compiler reacts the same way to the classes, which contains the const member. Changing a const member is not legal, so the compiler does not know how to confront it within its own generated assignment function. Finally, there is the case that 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. After all, the copy assignment operator that the compiler has built for derived classes can handle the base class component, but of course they cannot invoke the member functions that derived class does not have permission to invoke. The compiler can do nothing with his hands.

What functions the C + + compiler 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.