C + + constructors, copy constructors, destructors and assignment operators

Source: Internet
Author: User

Topic:

For the following rectangle class implementation constructor, copy constructor, assignment operator, destructor.

Class Shape

{

int no;

};

Class Point

{

int x;

int y;

};

Class Rectangle:public Shape

{

int width;

int height;

Point * LEFTUP;

Public

Rectangle (int width, int height, int x, int y);

Rectangle (const rectangle& Other);

rectangle& operator= (const rectangle& Other);

~rectangle ();

};

Analytical:

A constructor:

1. Use the initialization list as much as possible;

2. For the construction of the Leftup pointer, Leftup points to a point object, and the constructor needs to generate a new point object within the heap, pointing to the object with Leftup

inline rectangle::rectangle (int width,int heigt, int x,int y): Width (width), height (height), Leftup (new Point (x, y)) {}  //Use initialization list as much as possible, including initialization of Leftup  

Two-copy constructors:

1. Use the initialization list as much as possible

2. Note the copy construction of No to the parent class, by invoking the copy constructor of the parent class shape (other)

3. For the copy construction of the This->leftup, call the copy constructor of point to avoid extensive modifications when there is a change inside the point.

4. For a discussion of whether other.leftup is empty, null pointers are not necessary to generate objects within the heap, and when Other.leftup is empty, This->leftup initially defaults to random values and assigns them to null pointers.

The complete copy constructor:

Inlinerectangle::rectangle (const rectangle& Other): Shape (Other), Width (other.width), height (other.height) {  //Pay attention to the copy construction of an inherited object no, by calling the copy constructor of the parent class     if (other.leftup! = null) {                         //Other.leftup is empty for discussion, NULL pointer condition does not need to generate objects within the heap         this->leftup = new Point (*other.leftup);    Call the copy constructor of point to avoid extensive modifications when there is a change inside the point.     }    else{        this->leftup = NULL;                          Leftup Initial default is a random value, to which he is assigned a null pointer.     }}

Three-assignment operators

1. Assignment operators often need to first determine their own assignment to the situation, to avoid memory leaks

if (this = = &other) {                        return *this;}

2. Call the assignment operator of the parent class to complete the assignment of the inherited part of the parent class by doing the following:

    Shape::operator= (other);     

3. Need to discuss whether Leftup,other.leftup is empty

When the Other.leftup is empty, the This->leftup space is released directly, and it is assigned to empty.

When the other.leftup is not empty,

If the this->leftup is not empty, then directly assigns the contents of the Other->leftup point to the content that this->leftup points to;

If This->leftup is empty, create a new Point object

rectangle& rectangle::operator= (const rectangle& Other) {    if (this = = &other) {                 // Assignment operators often need to first determine their own assignment to the situation, to avoid memory leakage         return *this;    }    Shape::operator= (other);     Call the assignment operator of the parent class to complete the assignment of the inherited part of the parent class     this->width = Other.width;    This->height = other.height;    if (other.leftup! = null) {        if (leftup! = null) {            *leftup = *other.leftup;             You do not have to delete the Leftup and rebuild, you can simply assign (dereference the pointer, invoke the assignment operator of the Point Class)         }        else{            leftup = new Point (*other.leftup);      Leftup is empty, cannot be unpacked, need to create a new object         }    }    else{        delete leftup;         This->leftup = NULL;     }    

Four-destructor function

Rectangle:: ~rectangle () {    

Five overall code and other considerations

1.Rectangle assignment Constructors, construction order: The Father class, followed by the order declared in the subclass, regardless of the order in the initialization list.

2. Correctly distinguish between copy constructors and assignment operators.

A copy constructor is a constructor, that is, when a new object is created, an object exists and an object does not exist;

When the assignment operator is used, two objects must be present, so the question to be discussed is whether to self-assign and so on.

3 in the face of this type of problem method:

Forget the grammar and draw the memory model first

This example is

And then write the time to analyze whether the pointer is empty;

Copy construction is one side, there is no; the assignment operator is both on both sides;

If the combination of pointers is empty, the above considerations can be analyzed.

class shape{int no;};     Class Point{private:int X;     int y;public:point (int x,int y): x (x), Y (y) {}};class rectangle:public shape{int width;     int height;     point* leftup;public:rectangle (int width, int height, int x, int y);     Rectangle (const rectangle& Other);     rectangle& operator= (const rectangle& Other); ~rectangle ();}; inline rectangle::rectangle (int width,int heigt, int x,int y): Width (width), height (height), Leftup (new Point (x, y)) {}// Try to use the initialization list, including the initialization inlinerectangle::rectangle (const rectangle& Other) for Leftup: Shape (Other), Width (other.width), Height (other.height) {//Note the copy construct for inherited object No, by calling the copy constructor of the parent class if (other.leftup! = NULL) {//For OTHER.L    Eftup whether the empty pointer condition is empty, it is not necessary to generate objects within the heap This->leftup = new Point (*other.leftup);     Call the copy constructor of point to avoid extensive modifications when there is a change inside the point.                          } else{this->leftup = NULL;     Leftup Initial default is a random value, to which he is assigned a null pointer. }}rectangle& rectangle::operator= (const Rectangle& other) {if (this = &other) {//Assignment operators often need to first determine their own assignment to the case, to avoid memory leakage return *this;     } shape::operator= (other);    Call the assignment operator of the parent class to complete the assignment of the inherited part of the parent class this->width = Other.width;    This->height = Other.height;             if (other.leftup! = null) {if (Leftup! = null) {*leftup = *other.leftup;      You do not have to delete the Leftup and rebuild, you can simply assign (dereference the pointer, invoke the assignment operator of the Point Class)} else{Leftup = new Point (*other.leftup);         Leftup is empty, cannot be unpacked, need to create a new object}} else{delete Leftup;     This->leftup = NULL; } return *this;}  Rectangle:: ~rectangle () {delete leftup;}

C + + constructors, copy constructors, destructors and assignment operators

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.