Summary of MTK test (1)

Source: Internet
Author: User

I am looking for a job this year. I ran to the MTK pen exam for the purpose of accumulating experience. The written test mainly covers C ++.
Because C ++ has been used in development, and is familiar with the advanced features in C ++: Object-oriented, templates, etc. It's Okay To study STL, BOOST, therefore, I am more confident in my C ++ level, so I did not make any preparations in advance, so I went directly to the test. After the written examination, I thought the question was quite simple. But after I came back with a serious learning attitude, I tried all the questions on the computer. The result was a tragedy, the wrong body is not complete...
Summary:
1. Take it seriously. Don't underestimate the written examination questions: When I was doing the questions, I thought that these written examination questions were very simple. Many questions were answered immediately after a glance, after the results are returned, we can find that all these questions have been set up with traps, so that you can fall into the trap.
2. C ++ has a solid foundation. I am still studying the advanced features of C ++ from one day to one night. As a result, many basic knowledge is completely unknown.
I will record some of my experiences and experiences in this test, so I can remind myself. The following describes some of my mistakes. The question is not exactly the same as the original question.
Question 1:
Int a = 10, B = 6;
Cout <a + B <"<a ++ <" "<B ++;
Describe the execution result of the preceding statement.
After reading this Code, many people will probably write the result of 16 10 6, but the output of the Computer Experiment is: 18 10 6.
Why is there such a result? The following is my analysis process. If there is something wrong, please correct it.
In order to track the code execution steps, I designed a Class X, which simulates the int. The behavior is basically the same as that of the int. Besides printing some information that helps us understand, the Code is as follows:

Class X
{
Public:
X () {cout <"default construct" <endl ;}
X (int a): I (a) {cout <"construct" <I <endl ;}
~ X () {cout <"desconstruct" <I <endl ;}
X (const X & x): I (x. I)
{
Cout <"copy construct" <I <endl;
}
X & operator ++ ()
{
Cout <"operator ++ (pre)" <I <endl;
++ I;
Return * this;
}
Const X operator ++ (int)
{
Cout <"operator ++ (post)" <I <endl;
X x (* this );
++ I;
Return x;
}
X & operator = (int m)
{
Cout <"operator = (int)" <endl;
I = m;
Return * this;
}
X & operator = (const X & x)
{
Cout <"operator = (X)" <endl;
I = x. I;
Return * this;
}
/////////////////////////
Friend ostream & operator <(ostream & OS, const X & x)
{
OS <x. I;
Return OS;
}
Friend X operator + (const X & a, const X & B)
{
Cout <"operator +" <endl;
Return X (a. I + B. I );
}
//////////////////////////
Public:
Int I;
};
Then run the following code:

X a (10), B (6 );
Cout <"sum:" <a + B <"a:" <a ++ <"B:" <B ++ <endl;

Use GCC4. 5. After compilation, the code execution result is as follows:


Construct 10
Construct 6
Operator ++ (post) 6
Copy construct 6
Operator ++ (post) 10
Copy construct 10
Operator +
Construct 18
Sum: 18 a: 10 B: 6
Desconstruct 18
Desconstruct 10
Desconstruct 6
Desconstruct 7
Desconstruct 11
Let's briefly analyze the execution process:


Construct 10
Construct 6 // The two rows of output correspond to X a (10), B (6 );


Operator ++ (post) 6
Copy construct 6 // indicates that cout <"sum:" <a + B <"a:" <a ++ <"B: "<B ++ <endl; the expression B ++ in this sentence,
The expression B ++ returns a temporary object with a value of 6, while the value B becomes 7.
Operator ++ (post) 10
Copy construct 10 // The analysis of this sentence is the same as above


Operator +
Construct 18 // corresponds to expression a + B. It can be seen that a and B have changed to 11 and 7. The expression returns a temporary object with a value of 18.


Sum: 18 a: 10 B: 6 // output result. It can be seen from the result that the actually printed values are a + B, respectively, the temporary variables returned by the expression a ++ and B ++.


Desconstruct 18 // structure of the Temporary Variable returned by expression a + B
Desconstruct 10 // Analysis of the temporary variables returned by a ++ expression
Desconstruct 6 // Analysis of the temporary variables returned by the B ++ expression
Desconstruct 7 // analysis structure of variable
Desconstruct 11 // Analysis of variable B

The truth is clear. Why does the compiler compile this expression like this?
The night breeze on the second floor below gave the correct answer .. In order not to mislead the later colleagues, I would like to edit it ..

The environment for the above experiments is GCC4. 5 according to my classmates, the execution results of VS2010 under DEBUG and RELEASE are: 16 10 6 and 18 10 6 respectively, but I have not verified it, if you are interested, you can verify and analyze it.
I have gained a lot from doing this and consolidated the foundation of C ++.
I wrote this article today, and I will give you some time to analyze other "traps" in the future.
(To be continued)

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.