C ++ constructor/destructor/assignment/copy Function Comparison

Source: Internet
Author: User
Constructors, destructor, and assignment functions are the most basic functions of each class. Each class has only one destructor, but multiple Constructors (including one copy constructor, and others are called normal constructor) and multiple value assignment functions (in addition to the same type of value assignment, there are other value assignment methods ). If you do not want to write the above functions for any class A, the C ++ compiler will automatically generate four default functions for Class A, such
A (void); // default no-parameter Constructor
A (const A & A); // default copy constructor
~ A (void); // default destructor
A & operate = (const A & A); // default value assignment function

There are several notes:
@ Another special feature of constructor and destructor is that there is no return value type
@ Starts from the top-level base class of the class hierarchy. In each layer, the base class constructor is called first, and then the member object constructor is called. The structure is executed in the reverse order of the structure. During the analysis, the destructor of the derived class at the lowest layer is first called, and then the destructor of each base class is called.
@ "Default copy constructor" and "Default Value assignment function" are implemented by "bit copy" instead of "value copy". If the class contains pointer variables, these two functions are doomed to go wrong.

The following example further explains,

1. Initialization table of the constructor
There are two classes:

Class
{
...
A (void); // No parameter Constructor
A (const A & other); // copy the constructor
A & operate = (const A & other); // value assignment function
Virtual ~ A (void); // destructor
};
Class B
{
Public:
B (const A & A); // constructor of B

PRIVATE:
A M_a; // member object
};

Below are two implementations of B's constructor. The first constructor of Class B calls the copy constructor of Class A in its initialization table, in this way, the member object M_a is initialized, while the second constructor of B initializes the member object M_a by assigning values in the function body. We only see a value assignment statement, but in fact, the constructor of B does two things: Create the M_a object in the dark (call the non-parameter constructor of ), call the value assignment function of Class A to assign parameter A to M_a.

B: B (const A &)
: M_a ()
{
...
}

B: B (const A &)
{
M_a =;
...
}

2. Differences between the copy function and the constructor
A copy constructor is called when an object is created, and a value assignment function can only be called by an existing object.
String A ("hello ");
String B ("World ");
String c = A; // call the copy constructor. It is best to write it as C ();
C = B; // The Value assignment function is called.
In this example, the third statement has a poor style and should be rewritten to string C (A) to distinguish it from the fourth statement.

If we really don't want to write the copy constructor and value assignment functions, and do not allow others to use the default functions generated by the compiler, we can declare the copy constructor and value assignment function as private functions without writing code.

3. destructor and virtual destructor
Constructors, destructor, and assignment functions of the base class cannot be inherited by the derived class. If there is an inheritance relationship between classes, pay attention to the following when writing the above basic functions:
@ The constructor of the derived class should call the constructor of the base class in its initialization table
@ The destructor of the base class and derived class should be virtual (that is, the virtual keyword is added)

# Include <iostream>
Class base
{
Public:
Virtual ~ Base () {cout <"~ Base "<Endl ;}
};
Class derived: public Base
{
Public:
Virtual ~ Derived () {cout <"~ Derived "<Endl ;}
};

Void main (void)
{
Base * pb = new derived; // upcast
Delete Pb;
}

Output result:
~ Derived
~ Base
If the Destructor is not virtual, the output result is
~ Base

Example:

Write string-like constructor, destructor, and value assignment function.

Class string {

Public:

String (const char * STR = NULL );

String (const string & other );

~ String (void );

String & operate = (const string & other );

PRIVATE:

Char * m_data;

};

String: string (const char * Str ){

If (STR = NULL ){

M_data = new char [1];

* M_data = '/0 ';

}

Else {

Int length = strlen (STR );

M_data = new char [Length + 1];

Strcpy (m_data, STR );

}

}

String: string (const string & Other ){

Int length = strlen (other. m_data );

M_data = new char [Length + 1];

Strcpy (m_data, other. m_data );

}

String ::~ String (void ){

Delete [] m_data;

}

String & string: operate = (const string & Other ){

If (this = & Other) // check the auto-assigned value.

Return * this;

Delete [] m_data; // release the original memory resource.

Int length = strlen (other. m_data );

M_data = new char [Length + 1];

Strcpy (m_data, other. m_data );

Return * this;

}

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.