Reading notes effective C + + Item 5 Understanding C + + default build and call functions

Source: Internet
Author: User

1 which functions are generated by the compiler by default  

When is an empty class no longer an empty class? The answer is an empty class that is handled in C + +. If you do not declare yourself, the compiler will declare their own version of the copy constructor for you, copy the assignment operators and destructors, and if you do not declare a constructor, the compiler will also declare a default copy constructor for you. All of these functions will be public and inline (ITEM30). So, if you write the following class:

Class empty{};

This is essentially the same as the following class:

1ClassEmpty {23Public:45 Empty () {...}//Default constructor6const empty& rhs) {...} // copy constructor 8  9 ~empty () {...} // Destructor-see below 10 11 // for whether it ' s virtual12 13 empty& operator= (const empty& rhs) {...} // copy assignment operator 14 15};        

These functions are generated only when they are needed. It is common to need these functions in the program. The following code causes the corresponding function to be generated:

default constructor; destructor// copy constructor// copy assignment operator /c3>

2 What does the default generated function do?

What do these functions do, given that the compiler generates functions for you? The default copy constructors and destructors give the compiler a place to put "hidden code behind the scenes", such as constructors and destructors for base and non-static data members. Note The resulting default destructor is not a virtual function (ITEM7) unless the class that generates the default destructor inherits from a base class that declares a virtual destructor (so that the default destructor is virtual or non-virtual inherited from the base class).

2.1 Default copy constructor

For copy constructors and copy assignment operators, the compiler- generated version simply copies the non-static data members of the source object to the target object . For example, consider a Namedobject template class that allows you to associate with objects of type T.

1 Template<typename t>23ClassNamedobject {45Public:67 Namedobject (ConstChar *name,Const t&Value); 9 Namedobject (const Std::string& name, const t& value); 10 11 ...12 13 private:14 15 std::string16 17 t objectvalue;< Span style= "color: #008080;" >18 19};            

Because a constructor is declared in Namedobject, the compiler no longer generates a default constructor for it. This is important, if you carefully design a class that requires a parameter constructor, you don't have to worry that the compiler will add a constructor with no parameters to overwrite your version.

Namedobject neither declares the copy constructor nor declares the copy assignment operator, so the compiler produces these functions (if they are needed). Look at the following example, the copy constructor is used in this way:

1 namedobject<int> No1 ("smallest Prime number2);  3 namedobject<// calls copy constructor       

The compiler-generated copy constructors must initialize No2.namevalue and No2.objectvalue, respectively, with No1.namevalue and No1.objectvalue. The type of Namevalue is string, the standard string type has a copy constructor, and No2.namevalue is initialized by invoking the copy constructor of string, which uses No1.namevalue as the parameter. In addition, the type of namedobject<int>::objectvalue is int, so no2.objectvalue is initialized by copying bits from No1.objectvalue.

2.2 Default Copy assignment operator

The behavior of the copy assignment operator generated by the compiler for namedobject<int> is essentially the same as the copy constructor. In general, however, the compiler-generated copy assignment operator is the same as the copy constructor only if the generated code is legitimate and has a reasonable chance of proving it to be meaningful . If you are dissatisfied with any of these conditions, the compiler will refuse to generate operator= for your class.

For example: Consider namedobject as defined below, Namevalue is a reference to a string, and Objectvalue is a const T.

1 Template<typename t>23ClassNamedobject {45Public:67//This ctor no longer takes a const name, because Namevalue89//is now a reference-to-non-const string. The char* constructor1011//Is gone, because we must has a string to refer to.12Namedobject (std::string& name,Const t&Value);1415 ...//16 17 // operator= is Declared18 19 private< Span style= "color: #000000;" >:20 21 std::string & Namevalue; // This was now a reference 22 23 const T ObjectValue; // This is now Const24 Span style= "color: #008080;" >25};                 

Consider what happens next:

1 std::String Newdog ("Persephone");23 std::String Olddog ("Satch");45 namedobject<Int> p (Newdog,2);//When I originally wrote this, we67//Dog Persephone was on to89// had her second birthday 10 11 namedobject< int> s (olddog, 36); // The family dog Satch (from My12 13 // childhood) would be She14 15 //  were still alive16 17 p = S //   

Before the assignment, both P.namevalue and S.namevalue point to the string object, although not the same. How does the assignment affect p.namevalue? After assignment, should P.namevalue point to the string that S.namevalue points to? That is, does the reference itself change? If so, it opens up a new World because C + + does not provide a way to point the reference to another object. Or, should the string object pointed to by P.namevalue be modified? This affects other objects that hold pointers or references to the modified string. This is the compiler-generated copy assignment operator.

In the face of this dilemma, C + + refuses to compile code. If you want to support a copy assignment operator in a class that contains a reference member, you must define a copy assignment operator yourself. For classes that contain const members, the behavior of the compiler is similar. Modifying a const member is not legal, so when a default copy assignment operator is generated, the compiler is unsure how to handle them. Finally, if the copy assignment operator is declared as private in the base class, the compiler refuses to generate a copy assignment operator in the derived class. After all, the copy assignment operators generated by the compiler for derived classes need to be able to handle the parts of the base class, in which case they are certainly not able to trigger a function that the derived class does not have permission to invoke.

Reading notes effective C + + Item 5 Understanding C + + default build and call functions

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.