通过一道面试题来看 C++ 语言中的表达式求值
生活随笔
收集整理的這篇文章主要介紹了
通过一道面试题来看 C++ 语言中的表达式求值
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
題目一:
int ?a = 10 ,b = 6 ;
cout << a + b << " ? " << a ++<< " ? " << b ++ ;?
請(qǐng)說出上述語句的執(zhí)行結(jié)果。
很多人看過這段代碼后估計(jì)都會(huì)直接就寫上了 16 10 6 這樣的結(jié)果吧,但上機(jī)實(shí)驗(yàn)的輸出結(jié)果是: 18 10 6
為什么會(huì)出現(xiàn)這樣的結(jié)果,下面是我的分析過程,如果有不對(duì)的地方請(qǐng)大家指正。
為了跟蹤代碼的執(zhí)行步驟,我設(shè)計(jì)了一個(gè)類X,這個(gè)類是對(duì)int的模擬,行為方面與int基本一致,除了會(huì)打印出一些幫助我們理解的信息,代碼如下:
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;
};
然后執(zhí)行以下代碼:
? ? X?a( 10 ),b( 6 );
????cout << " sum: " ? << a + b << " ?a: " << a ++<< " ?b: " << b ++<< endl;
使用GCC4。5編譯后,代碼的執(zhí)行結(jié)果如下:
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 我們來簡(jiǎn)單分析下這個(gè)執(zhí)行過程:
construct 10 construct 6 ?//這兩行輸出對(duì)應(yīng)于 X a(10),b(6);?
operator ++(post) 6 copy construct 6 //表明首先執(zhí)行了??cout<<"sum:" <<a+b<<" a:"<<a++<<" b:"<<b++<<endl;這句中的 b++這個(gè)表達(dá)式,
??????????????????????????????b++這個(gè)表達(dá)式返回了一個(gè)值為6的臨時(shí)對(duì)象,而b本身則變成了7。
operator ++(post) 10?
copy construct 10 ?//這句的分析同上
operator + construct 18 //對(duì)應(yīng)于表達(dá)式 a+b ,可以看到,此時(shí)的a和b已經(jīng)變成了11和7。表達(dá)式返回了一個(gè)值為18的臨時(shí)對(duì)象。
sum:18 a:10 b:6 //輸出的結(jié)果,從結(jié)果可以看出,實(shí)際上打印出的值分別為 a+b,a++和b++三個(gè)表達(dá)式所返回的臨時(shí)變量。
desconstruct 18 //a+b 表達(dá)式返回的臨時(shí)變量的析構(gòu) desconstruct 10?//a++ 表達(dá)式返回的臨時(shí)變量的析構(gòu) desconstruct 6?//b++表達(dá)式返回的臨時(shí)變量的析構(gòu) desconstruct 7?//變量a 的析構(gòu)
desconstruct 11 ?//變量b的析構(gòu)
真相大白了。為什么編譯器會(huì)這樣來編譯這個(gè)表達(dá)式呢?
其實(shí)<<在同一語句中連續(xù)使用,其實(shí)本質(zhì)上是函數(shù)的復(fù)合調(diào)用:cout<<a+b<<" "<<a++<<" "<<b++;?
本質(zhì)上是
operator<<(operator<<(operator<<(cout,a+b),a++),b++)
由于c++函數(shù)的參數(shù)的求值順序是從右至左的(c++標(biāo)準(zhǔn)雖未規(guī)定,但是基本所有的編譯器是這么干的),所以參數(shù)的計(jì)算次序是:
b++ //7
a++ //11
a+b //18
cout<<18
cout<<10 //因?yàn)?0已經(jīng)先入棧了
cout<<6//同上
上述實(shí)驗(yàn)的環(huán)境均為GCC4。5 ?據(jù)同學(xué)說VS2010執(zhí)行的結(jié)果在DEBUG下和RELEASE下居然分別為:16 10 6 和18 10 6,不過我沒有去驗(yàn)證過,有興趣的同學(xué)可以去驗(yàn)證并分析一下。
附上一篇專門講解C/C++表達(dá)式求值的文章。http://blog.csdn.net/luciferisnotsatan/article/details/6456696
轉(zhuǎn)載于:https://my.oschina.net/u/90679/blog/109042
總結(jié)
以上是生活随笔為你收集整理的通过一道面试题来看 C++ 语言中的表达式求值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给网页添加二维码功能
- 下一篇: Oracle 返回结果集 sys_ref