Member functions implicitly declared by default for empty classes

Source: Internet
Author: User

The problems I found on the Forum recently have changed my previous incorrect views in this regard and I will share them with you.
It is said that this question has a high chance of being asked during the interview. Fortunately, I have never encountered it.
The popular view is:

Class Empty {};
Equivalent:

Class Empty
{
Public:
Empty ();
Empty (const Empty &);
~ Empty ();
Empty & operator = (const Empty & rhs );
Empty * operator &();
Const Empty * operator & () const;
};
Based on the descriptions in Chapter 12 Special member functions in ISO/IEC 14882: 2003 (E), it can be determined that
The default constructor copy constructor operator = is implicitly declared. Empty * operator & () and const Empty * operator & () const; are not implicitly declared.

The source of the error is translated by Hou Jie's simplified Chinese Simplified "negative c ++, 2nd".
Clause 45: Find out the functions written and called by C ++ behind the scenes
When is an empty class not an empty class? ---- When the C ++ compiler passes through it. If you do not declare the following functions, the considerate compiler will declare its own version. These functions are: A copy constructor, a value assignment operator, a destructor, and a pair of access operators. In addition, if you do not declare any constructor, it will also declare a default constructor for you. All these functions are public. In other words, if you write:

Class Empty {};
This is the same as writing:

Class Empty {
Public:
Empty ();
Empty (const Empty & rhs );
~ Empty ();
Empty & operator = (const Empty & rhs );
Empty * operator &();
Const Empty * operator & () const;
};
Some readers have raised a question to Scott Meyers, the author of the book. Scott Meyers also thinks that the answer above is problematic.
The author's explanation of this problem is as follows:
! 2/10/00 ic 212 A class declaring no operator & function (s) 9/10/01
Cxh 213 does NOT have them implicitly declared. Rather,
245 compilers use the built-in address-of operator
246 whenever "&" is applied to an object of that
Type. This behavior, in turn, is technically
Not an application of a global operator &
Function. Rather, it is a use of a built-in
Operator.
So far, I believe that the reader has an answer.

From Jiang Yafeng's CSDN blog

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.