日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

通过一道面试题来看 C++ 语言中的表达式求值

發布時間:2024/4/14 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过一道面试题来看 C++ 语言中的表达式求值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>


題目一:
int ?a = 10 ,b = 6 ;
cout
<< a + b << " ? " << a ++<< " ? " << b ++ ;?
請說出上述語句的執行結果。
很多人看過這段代碼后估計都會直接就寫上了 16 10 6 這樣的結果吧,但上機實驗的輸出結果是: 18 10 6
為什么會出現這樣的結果,下面是我的分析過程,如果有不對的地方請大家指正。
為了跟蹤代碼的執行步驟,我設計了一個類X,這個類是對int的模擬,行為方面與int基本一致,除了會打印出一些幫助我們理解的信息,代碼如下:

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;
};

然后執行以下代碼:

? ? X?a( 10 ),b( 6 );
????cout
<< " sum: " ? << a + b << " ?a: " << a ++<< " ?b: " << b ++<< endl;

使用GCC4。5編譯后,代碼的執行結果如下:

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 我們來簡單分析下這個執行過程:
construct 10 construct 6 ?//這兩行輸出對應于 X a(10),b(6);?

operator ++(post) 6 copy construct 6 //表明首先執行了??cout<<"sum:" <<a+b<<" a:"<<a++<<" b:"<<b++<<endl;這句中的 b++這個表達式,
??????????????????????????????b++這個表達式返回了一個值為6的臨時對象,而b本身則變成了7。
operator ++(post) 10?
copy construct 10 ?//這句的分析同上

operator + construct 18 //對應于表達式 a+b ,可以看到,此時的a和b已經變成了11和7。表達式返回了一個值為18的臨時對象。

sum:18 a:10 b:6 //輸出的結果,從結果可以看出,實際上打印出的值分別為 a+b,a++和b++三個表達式所返回的臨時變量。
desconstruct 18 //a+b 表達式返回的臨時變量的析構 desconstruct 10?//a++ 表達式返回的臨時變量的析構 desconstruct 6?//b++表達式返回的臨時變量的析構 desconstruct 7?//變量a 的析構

desconstruct 11 ?//變量b的析構

真相大白了。為什么編譯器會這樣來編譯這個表達式呢?

其實<<在同一語句中連續使用,其實本質上是函數的復合調用:
cout<<a+b<<" "<<a++<<" "<<b++;?
本質上是
operator<<(operator<<(operator<<(cout,a+b),a++),b++)
由于c++函數的參數的求值順序是從右至左的(c++標準雖未規定,但是基本所有的編譯器是這么干的),所以參數的計算次序是:
b++ //7
a++ //11
a+b //18
cout<<18
cout<<10 //因為10已經先入棧了
cout<<6//同上

上述實驗的環境均為GCC4。5 ?據同學說VS2010執行的結果在DEBUG下和RELEASE下居然分別為:16 10 6 和18 10 6,不過我沒有去驗證過,有興趣的同學可以去驗證并分析一下。

附上一篇專門講解C/C++表達式求值的文章。http://blog.csdn.net/luciferisnotsatan/article/details/6456696


轉載于:https://my.oschina.net/u/90679/blog/109042

總結

以上是生活随笔為你收集整理的通过一道面试题来看 C++ 语言中的表达式求值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。