Effective C + + clauses 05, 06 (Compiler auto-generated function) collation

Source: Internet
Author: User

The compiler is the prototype of the empty class generation function and the time when the function is created

In C + + when creating an empty class, C + + creates 4 functions for the class by default: The default constructor, destructor, copy constructor, and assignment operator.

Create an empty class in C + +:
Class Empty {};
By default, 4 functions are generated and their functions are prototyped as follows:

Public:empty () {...}    Empty (const empty& RHS) {...}    ~empty () {...} empty& operator= (const empty& RHS) {...}

Description
1) These functions will only be generated by the compiler when calls are required.
2) 4 functions are public.
3) 4 functions are inline (that is, functions defined in the definition of a class).
4) If you explicitly declare any of these functions, the compiler will no longer generate a default function.

For example, when the following statement is encountered, the function is generated by the compiler:

Empty E1;                         when the default constructor//object is destroyed, the destructor empty E2 (E1);            Copy constructor e2 = E1;                 Assignment operators
In addition, there are two default functions: The const version of the address operator and the fetch address operator, which are not mentioned in effective C + +.

public:empty* operator& () {...} Const empty* operator& () const {...}

These two functions do exist, just as the following code works correctly:
#include <stdio.h>class Empty {};int main (int argc, char** argv) {       empty A;               Const Empty *B = &a;        printf ("%p/n", &a);             Call the FETCH address operator       printf ("%p/n", b);              Call the const take address operator}

An easy-to-ignore problem: A custom copy constructor overrides the default copy constructor and overrides the default constructor. The following code is compiled, but the user must then explicitly define a parameterless constructor.

Class Empty{public:          empty (const empty& e) {}    //Copy constructor};int main (int argc, char** argv) {        empty A;}

Second, the problem of automatically generated assignment operator

The behavior of the assignment operator function is basically the same as the behavior of the copy constructor, and the compiler generates an assignment operator function that is conditional, and the compiler will refuse to produce the function if it produces an operation that cannot be completed. So when does the compiler fail to complete the assignment? Consider the following scenario (source effective C + +):

Template<class T>class nameobject {public:        nameobject (std::string& name, const t& value);p rivate:        std::string& namevalue;    Reference member variable        const T objectValue;       The const member variable};//then considers what happens with the following statement: std::string Newdog ("abc"); std::string olddog ("xxx"); Nameobject<int> p (Newdog, 2); nameobject<int> s (Olddog, ten);p = s; What's going to happen?
Before the assignment statement, P.namevalue points to Newdog, S.namevalue points to Olddog. What about after the assignment? Should the p.namevalue point to the object that the S.namevalue points to?
But C + + has one rule: references cannot be changed to another object.
For variable objectvalue,c++, it is illegal to change a const member.
So if any of the above two scenarios occur, the C + + compiler gives a response that denies compiling the assignment of this line. If you do this, the C + + compiler will make an error.
If you are determined to perform an assignment, you can define an assignment operator overload function yourself.

Third, hide the compiler automatically generated functions

In order to dismiss the function provided by the compiler automatically (secretly), the corresponding member function can be declared private and not implemented, as follows

Class homeforsale{public  :      ...  Private:     Homeforsale (const homeforsale&);     homeforsale& operate= (const homeforsale&);  There is no need to write parameter names at this time. Because no implementation is required};
Since the copy constructors and assignment functions of the compiler output are public, we need to declare them ourselves in order to prevent them from being created, but we declare them as private, which explicitly prevents the compiler from secretly creating proprietary versions, and makes these functions private. Makes it possible to successfully prevent others from calling it
Of course, function members and friends can still be called, just do not implement these functions, this method is simple and easy to use, the other way in the book multiple inheritance is easy to cause confusion, not optimistic.

Iv. new changes of c++11

The concept of "rvalue reference" and "Move Semantics" are introduced in c++11, which enables a reference to Rvalue. (The interpretation of lvalue and rvalue can be seen in http://amyz.itpub.net/post/34151/411832)
Moving semantics, simply put, is to steal the resources of a right-valued object before the end of its lifetime, for me. For more detailed information on moving semantics, please refer to csdn previous article http://blog.csdn.net/pongba/article/details/1684519. The move constructor and the move assignment operator are described here.
1. The move constructor and move assignment operator overloaded functions are not implicitly declared and must be defined by themselves.
2. If the user has customized the copy constructor or the move constructor, the default constructor will not be implicitly defined, and if required by the user, an explicit definition is required.
3. The move constructor does not overwrite an implicit copy constructor.
4. The move assignment operator overload function does not override an implicit assignment operator overload function.


Effective C + + clauses 05, 06 (Compiler auto-generated function) collation

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.