Summary of replication control members

Source: Internet
Author: User

1. Replication control members refer to: copying constructor, value assignment operator function, and destructor.

2. For more information about the replication constructor, see replicate constructor summary.

3. If you do not have your own assignment operator function, the compiler will provide one.

The value assignment operator also copies each non-static member in sequence, which is also a shallow copy.

The difference between the value assignment operator and the copy structure is that it does not need to open up new spaces for objects.

4. The order of the Destructor object is the reverse order of the created object, that is, the reverse order of the declared order

Example:

 
Class A {int X; public: A (int I) {x = I; cout <"construct a" <I <Endl ;}~ A () {cout <"delete a" <x <Endl ;}}; Class B {A; a B; a c; public: B (): C (3), B (2), a (1) {cout <"construct B" <Endl ;}~ B () {cout <"delete B" <Endl ;}}; int main () {B; return 0 ;}

Output result:

Construct a 1
Construct a 2
Construct a 3
Construct B
Delete B
Delete A 3
Delete A 2
Delete A 1

The order of the initialization list is different here. We can see that the order of the structure and structure is irrelevant to the initialization list, and it is only related to the declared order.

5. Unlike other functions, that isIf you define your own destructor, the default destructor will be executed.

Run your own destructor first, and then run the default destructor.

6. In the replication control function, only the content is copied during replication.

For example, if you copy a pointer, you can only copy the address in the pointer, instead of the content pointed to by the pointer.

When the class contains pointer members, an error occurs in the shallow copy. You need to define your own deep Replication

If you need to define your own destructor, you must define all of your own three control member functions.

7. define your own control member functions

 
Class A {int val; int * PTR; public:/* constructor PTR (New int (p): creates an int with the same content as p, PTR points to the int */A (const Int & P, int I): PTR (New int (p), Val (I) {}/* copy constructor * orig. PTR: returns the pointer orig. content PTR pointed to by PTR (New int (* orig. PTR): creates an int, Int, and * orig. PTR is the same. PTR points to the int */A (const A & orig): PTR (New int (* orig. PTR), Val (orig. val) {}/* The Value assignment operator does not need to open up space */A & operator = (const A & orig) {/* is the meaning of the function content rig. the content pointed to by PTR is assigned to the content pointed to by PTR */* PTR = * orig. PTR; val = orig. val; return * This;} // destructor ~ A () {Delete PTR ;}};

In this example, this object can access the Private Members of the orig object.

The private definition is as follows: only the creator of the class (this here) and the member functions of the class can access

What I understand is that access is a private member of the Creator.

In this example, why can this object access the Private Members of the orig object? Explanations

 

8. If the class contains pointer members, the replication control function must be written by itself.
Sometimes, the application of replication control is not obvious and easy to ignore.
Therefore, when a class contains pointer members, you must carefully analyze the usage of the class. You cannot ignore the use of any possible replication control.
The safest way is to write and control data as long as you see pointer members in the class.

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.