一些看到的c++面試題 自己試着回答一下

1、T是一個class,T a=b;和 T a; a=b; 有什麼不同?
前者: 調用複製構造函數。
後者: 調用 重載的賦值操作符函數
2、重載T的賦值操作符時參數和返回值分別是什麼?
  1. void Date::operator=(const Date& dt)  
  2. {  
  3. if (this != &dt)   
  4. {  
  5. mo = dt.mo;  
  6. da = dt.da;  
  7. yr = dt.yr;  
  8. delete [] month;  
  9. if (dt.month != NULL)  
  10. {  
  11. month = new char [std::strlen(dt.month)+1];  
  12. std::strcpy(month, dt.month);  
  13. }  
  14. else 
  15. month = NULL;  

  16. }  
  17. }  

3、class T1有一個char成員變量,class T2有一個char成員變量還有個void f();成員函數,在4字節對齊的情況下,sizeof(T1)和sizeof(T2)分別是多少?
4   4

4、class T
{
 public:
 void f1(){...}
 void f2()const{...}
 };
問f1和f2的區別?
f1()可以修改成員變量。
f2()不可以修改成員變量。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章