C ++'s old curse

Source: Internet
Author: User


In other words, our teachers kept learning when they first came into contact with the C language! Constantly !! Constantly !!! The main function is divided into five parts:

1. Declaration + initialization 2. Assignment 3. Input 4. Output 5. Release

I don't understand why we need to separate the "Declaration + initialization" and "assignment" steps.
Will "initialization + assignment" Be cursed together?
Obviously, there is a program like this: int I = 10; what's more, directly int I, j = 1, k = 10; even the sound is added with a value, you can do it directly.
Why did Cao force us to write 10 lines of code written by others ......

// Initialization
Int I = 0
Int j = 0;
Int k = 0;


// Assignment
I = 0;
J = 1;
K = 10;

Why do we need to put forward the assignment step separately? Does this prevent a special curse ?? Yes!
Today, I found that VC6.0 and VS2008 contain such a "curse "~

First, we write such a simple code

[Cpp]
Class Example
{
Private:
Int m_data1;
Int m_data2;
Int * m_pdata3;
 
Public:
Example ();
Void setData (int given1, int given2, int given3 );
};
 
Example: Example ()
{
M_data1 = 0;
M_data2 = 0;
M_pdata3 = new int;
* M_pdata3 = 0;
}
 
Void Example: setData (int given1, int given2, int given3)
{
M_data1 = given1;
M_data2 = given2;
* M_pdata3 = given3;
}
 
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
Example e1;
 
E1.setData (10, 20, 30 );
 
Example e2;
 
E2 = e1; www.2cto.com
 
E2.setData (40, 50, 60 );
 
Return 0;
}

The execution is successful.
Here, we first define the Example class e1; then the Example class e2. We didn't overload the value assignment operator =, but the compiler found a way to implement the e2 = e1 operator. As you can understand, the system creates a value assignment function by default before the value = is reloaded. Here, we call this assignment function the default assignment function.

Next, let's take a look at the function of the default value assignment function.
First, after e1.setData (10, 20, 30), it is monitored that e1 is like this:

When e2 = e1 is assigned a value, the monitoring window of e2 is as follows:


We can see that the pointer m_pdata3 in e1 and e2 points to the same bucket. The following operations prove this better:
When modifying the internal data of e2 in e2.setData (40, 50, 60), the memory corresponding to the internal pointer of e1 is also changed to 60.

 

From this, we can infer that the default value assignment function has the following functions: Assign the value of the member variable of the class on the right to the same member variable in the class on the left.

The value assignment operator has the default value assignment function.
Next we will manually assign a value to the overload function of the Example Class.
[Cpp]
Example & Example: operator = (const Example & e)
{
M_data1 = e. m_data1;
M_data2 = e. m_data2;
M_pdata3 = new int;
* M_pdata3 = * (e. m_pdata3 );
 
Return * this;
}

Of course, we do not want to change the data in the e2 pointer. e1 also changes. In other words, the space opened up by this type of object is exclusive to this type of object. Therefore, when the space is reloaded, the value in the previous space is assigned to the new space.
The main function remains unchanged:
[Cpp]
Int _ tmain (int argc, _ TCHAR * argv [])
{
Example e1;
 
E1.setData (10, 20, 30 );
 
Example e2;
 
E2 = e1;
 
E2.setData (40, 50, 60 );
 
Return 0;
}

After executing e2.setData (40, 50, 60), we will find that through the monitoring window:

Indeed, the space opened by e2 has become 60, while e1 has not changed. .


Well, with these reserves, I believe you will easily understand this "curse:
When you put the two sentences of the Main Function
Example e2; // Initialization

E2 = e1; // value assignment
The value assignment and initialization are combined into one, that is, after Example e2 = e1
The main function is as follows:
[Cpp]
Int _ tmain (int argc, _ TCHAR * argv [])
{
Example e1;
 
E1.setData (10, 20, 30 );
 
Example e2 = e1;
 
E2.setData (40, 50, 60 );
 
Return 0;
}

Let's take a look at the program execution results:

We will find that e2 does not open up space independently, but points to the e1 space.
When the original value assignment and initialization are combined, when the = Operator is called, The = Operator function is not overloaded, but the default value assignment function!


This is the curse that combines initialization and assignment! Without distinction, unexpected problems may occur one day!

 

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.