Deep Exploration C + + object Model (5)

Source: Internet
Author: User
Tags bitwise constructor object model

We have made an understanding of the constructor of the synthetic province, which we continue to look at the interesting thing about the constructor.
What is Copy constructor? We often see some of these functions called in the Code X (x&) ("X of x ref"). This function is used as a parameter by the user-defined type, and its parameters are constructed by copy constructor. This thing is very important, actually copy constructor is synthesized by the compiler automatically, does not need you to do anything, but what does the compiler do? Our problem comes out.
There are three situations in which we need to use the contents of one object as the initial value of another class object. That is, we need compilers to automatically synthesize copy constructor for us. One of the things that we definitely reuse in programming is the class-generated object, such as the following:

Class classa{...}
ClassA A;
ClassA b=a; One class object does the initial value with another object

Another scenario is to pass the following pseudo code in the function as an object parameter:

For example we have a Cuser class
cuser{
Cuser ();
......
};
We also have a CDatabase class, it has a AddNew method
cdatabase{
......
Public
AddNew (Cuser userone);
......}
We use the Cuser class to produce an object instance. Userone and takes him as a parameter to the AddNew function so
The AddNew function completes adding a record to the database to record a user's information
CDatabase db=new CDatabase ();
Db. AddNew (Cuser userone)//Here, you don't have to expand the membership of your user class.

There is also a function of course return, for example, you can add a function in the CDatabase class to read a user's information such as Cuser getuserone (int userID), through a user's unique number can obtain a user's information, and returns an object of the Cuser class.

Let's see how copy constructor works. First copy constructor and default constructor are created by the compiler when needed, a class that does not declare a copy Constructor will have an implied statement (or definition), which is also divided into trivial and nontrivial two species.

Let's look at the examples in the book:

Class Word
{
Public
Word (const char*);
~word () {delete [] str;}
Private
int cnt;
Char *str;
}
The declaration of this class does not need to synthesize the default Copy constructor. But when applied as follows:
#include "Word.h"
Word noun ("lsmodel");
void Foo ()
{
Word Verb=noun;
}

The result will be disastrous. Why? Because our logical objects verb and global object noun all point to the same string, verb performs destructors before exiting the function foo (), the string is deleted, This global object Nonu points to a bunch of meaningless things. You can declare a explicit copy constructor to solve the problem, and of course you can have the compiler come from the move to synthesize a copy construct.

Let's rewrite the word class as follows:

Class Word
{
Public
Word (const string&);/Note this is the same as the X (x&) Form We started
~word ();
//......
Private
int cnt;
String str; This member is the object of the string class, and string is our custom type
};
Class String
{
Public
String (const char*);
String (const string&);//here declares a copy Constructir
~string ();
//......
}
At this time, executing our code
#include "Word.h"
Word noun ("lsmodel");
void Foo ()
{
Word Verb=noun;
}

The compiler will synthesize a copy constructor for our word class to invoke its STR (Member class String object) copy constructor. This is what the following pseudo code says:

Inline Word::word (const Word &WD)
{
Str. String::string (WD.STR);
cnt=wd.cnt;
}

When there is one or more virtual functions in this class, or the class is derived from a chain of inherited strings, and there is one or more virtual base classes in the string. This class does not show successive copies when it is copied (bitwise copy). And it's going to pass the synthetic copy constructor to explicitly set the vptr to point to the virtual function table, instead of copying the vprt of the right object directly. A picture of the zooanimal example in the book can clearly describe this.

What happens if an object makes an initial value with another object, and the latter has a virtual base class Subobject? Any compiler will do the location of the virtual base class Subobject in the derived class object is ready at the execution time , but bitwise copy may break this location, so it's also up to the compiler to synthesize a copy constructor to place some code to set the virtual base class Pointer/offset, Perform the necessary memberwise initialization for each member, and perform memory-related work.

Finally, let's sum up what we said above, and it's a bit messy. Thor increasingly felt his lack of words to describe the ability.

Our study is about what happens when an object takes on another object as its initial value.

Divided into two situations, one is we declare explicit copy constructor, this is not the article need to understand (I think we all understand). What we want to know is that we didn't declare explicit copy for class. What the compiler does when constructor functions. The compiler will synthesize a copy constructor for us. To accommodate any time when the object is properly initialized. And we understand that there are four kinds of situations in which class is not copied individually.

1. When you design a class that declares a explicit copy constructor function.
2. When you design a class that is derived from a base class that has a explicit copy constructor.
3. When you design a class that declares one or more virtual functions.
4. When you design a class that derives from a chain of inherited strings, the inheritance chain has one or more virtual base classes.

All right, let's just go here, have a rest and have a rest.

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.