"Effective C + +" study notes-clause 05

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************



Ii. Constructors,destructors and Assignment Operators

from the first chapter to understand C + +, began to enter the second chapter of the study, this chapter is mainly about the construction, destruction and assignment operations. For each class, there will be one or more constructors, a destructor, and a copy assignment operator. These things for a C + + programmer, is the most basic livelihood tools, the behavior of these functions will have a great impact on your program, so this chapter provides good guidance, let you combine these functions well together to form classes pillars.


Rule 05:know What functions C + + silently writes and calls

Rule 05: Understand what programs C + + silently writes and calls


1. An empty class--empty class

The first thing to understand is, if we write an empty class, when C + + deals with it, what does it produce or call a function?

For example, we write a class:

Class Empty {};

While C + + handles it, you don't write anything, but it's the equivalent of:

Class Empty{public:    Empty ()  {...} <span style= "White-space:pre" ></span>//default constructor    Empty (const empty& RHS)  {...} <span style= "White-space:pre" ></span>//copy constructor    ~empty ()  {...} <span style= "White-space:pre" ></span>//destructor (whether for virtual, later argument)    empty& operator= (const Empty & RHS)  {...} <span style= "White-space:pre" ></span>//copy assignment operator};

Yes, that's it, even if you don't write anything, C + + will automatically give this empty class, fill in these things. Of course, there are some things that can be covered by us, or some things will not be generated according to the content of the class members, which will be described later.

We need to know that for most classes, these things are necessary: the default constructor, the copy constructor, the destructor, the copy assignment operator, and these functions are both public and inline.


It is also important to be clear that, although the class has "added" so many things, it is not a call object that uses this function, such as:

When you create an empty class object

Empty E1;

At this point, the default constructor and destructor are output by the compiler.

And when this happens:

Empty E2 (E1);    The copy constructor is output e2 = e1;         Copy assignment operator is output


2. What are the effects of these things in class?

<1> for the default constructor and destructor

The main point is to give the compiler a place to put "behind the scenes" code, such as calling the constructors and destructors of base classes and non-static member variables. (Note: The destructor of the compiler output is non-virtual, unless the base class of the class itself declares a virtual destructor)

<2> for copy constructors and copy assignment operators

The version created by the compiler is simply a copy of each of the Non-static member variables of the source object to the target object at a later time.


3. Copy constructor and copy assignment operator

For a Namedobject template class, it allows you to associate a name and an object of type T

Template<typename> Tclass namedobject  {public:    namedobject (const char* name, const t& value);    Namedobject (const std::string& name, const t& value);    .... Private:    std::string namevalue;    T ObjectValue;};
☆ps: Because a constructor is declared in this class, the compiler no longer creates a default constructor for it. ☆

As you can see, the copy constructor is not declared in the class above, and the copy assignment operator is not declared, so if they are called, the compiler will create these functions for it.

copy function call:

namedobject<int> No1 ("Smallest Prime number", 2); Namedobject<int> No2 (No1);    Call the copy constructor

Let's see how the compiler operates on this function.

First, the compiler-generated constructor must set the two member variables of NO2 with the initial value of the two member variables Namevalue and Objectvalue of No1.

In both, the Namevalue type is string not a built-in type, and the standard string has a copy constructor, so no2.namevalue initialization is the copy constructor that calls string and takes No1.namevalue as an argument.

The other member type is the built-in type int, so no2.objectvalue is initialized with "copy every bits within No1.objectvalue".


For the copy assignment operator, the following two conditions are met, consistent with the copy constructor.

① generated code is legal

② has the right opportunity to prove it's meaningful.

Otherwise the compiler will refuse to produce operator for class =


4. Next, continue to see an example:

Template<class T>class namedobject  {public:    namedobject (std::string& name, const t& value);    . .. Pirvate: '    std::string& namevalue;    Const T ObjectValue;};

See, Namevalue became a reference type, Objectvalue became a const,

So, if you do this, do the following:

std::string Newdog ("Persephone"); std::string olddog ("Satch"); Namedobject<int> p (newdog,2); namedobject<int> s (olddog,36);p =s;

Most importantly, what happens when you execute the p=s line?

P.namevalue the string that points to s.namevalue?

Of course not!

C + + does not allow "let reference point to different objects"


So, in this case, how to solve it?

The response of C + + is to refuse to compile the line assignment action.

In fact, the following three cases, C + + will be "powerless"

<1> intends to support assignment operations within the class of a "content reference member", in which case the copy assignment operator must be defined by itself

<2> in the case of classes with const members, the compiler reacts as well, because it is illegal to change a const member.

<3> again if a base classes declares the copy assignment operator as private, the compiler will refuse to generate a copy assignment operator for its derived classes.


5.Please Remember

The compiler can secretly create a default constructor, a copy constructor, a copy assignment operator, and a destructor for class.




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

"Effective C + +" study notes-clause 05

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.