c++ 面試題(C/C++/STL)

1,智能指針:auto_ptr(c++11 已經棄用),unique_ptr(用於取代 auto_ptr),  shared_ptr,  weak_ptrhtml

  http://www.cnblogs.com/TenosDoIt/p/3456704.html(值得一看)ios

  https://blog.csdn.net/zhourong0511/article/details/80315961(優缺點分析)c++

 1 // classTest.cpp : 定義控制檯應用程序的入口點。  2 //  3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <string>
 7 #include <memory>
 8 
 9 using namespace std; 10 
11 class Test 12 { 13 public: 14     Test(string s); 15     ~Test(); 16     string& getStr(); 17     void setStr(string s); 18     void print(); 19 
20 private: 21     string str; 22 }; 23 
24 Test::Test(string s) 25 { 26     str = s; 27     cout << "Test creat" << endl; 28 } 29 Test::~Test() 30 { 31     cout << "Test delete:" << str << endl; 32 } 33 string& Test::getStr() { 34     return str; 35 } 36 void Test::setStr(string s) { 37     str = s; 38 } 39 void Test::print() { 40     cout << str << endl; 41 } 42 
43 // unique_ptr 使用
44 unique_ptr<Test> fun() { 45     return unique_ptr<Test>(new Test("789")); 46 } 47 
48 int _tmain(int argc, _TCHAR* argv[]) 49 { 50     cout << "==========unique_ptr===========" << endl; 51 
52     unique_ptr<Test> ptest(new Test("123")); 53     unique_ptr<Test> ptest2(new Test("456")); 54     ptest->print();  // 123
55     ptest2 = move(ptest);  // ptest == null,ptest2 原內容釋放,ptest2 接管ptest
56     if (ptest == nullptr) 57         cout << "ptest == nullptr" << endl; 58     Test* p = ptest2.release(); // 可能有返回值,返回當前指向的內存空間
59     p->print(); // 
60  ptest.reset(p); 61     ptest->print(); 62     ptest2 = fun(); 63     ptest2->print(); 64     cout << "===========shared_ptr===========" << endl; 65     shared_ptr<Test> sptest(new Test("123")); 66     shared_ptr<Test> sptest2(new Test("456")); 67     cout << sptest2->getStr()<< endl; 68     cout << sptest2.use_count() << endl; 69     sptest = sptest2; // "456"引用次數 +1,"123" 引用次數 -1(此時爲 0 ,被銷燬)
70     sptest->print(); // 456
71     cout << sptest2.use_count()<< endl; // 此時引用數爲2
72     cout << sptest.use_count() << endl;// 指向資源的引用數 爲 2
73     sptest.reset(); //引用數 -1
74     sptest2.reset(); //引用數 -1 ,此時爲 0 ,被銷燬
75     cout << "done" << endl; 76     system("pause"); 77     return 0; 78 }
smart pointer

2,智能指針的不足和缺陷:安全

  https://www.zhihu.com/question/61008381ide

 3,C++11 新特性:函數

  http://www.cnblogs.com/George1994/p/6684989.html(值得一看)spa

4,虛繼承,虛函數表:.net

  https://www.cnblogs.com/fanzhidongyzby/archive/2013/01/14/2859064.html(值得一看)線程

5,C 內存模型:設計

  https://blog.csdn.net/anyaas/article/details/17099377

  C++ 內存模型:

  https://www.cnblogs.com/findumars/p/5929831.html?utm_source=itdadao&utm_medium=referral(值得一看)

 6,若是在一個函數內定義一個 static 對象,何時執行構造和析構函數,若是這個函數從沒被調用,會怎麼被析構?

 1 #include <iostream>
 2 #include <string>
 3 #include <memory>
 4 
 5 using namespace std;  6 
 7 class Test {  8 public:  9  Test() { 10         cout << "Constructor is executed" << endl; 11  } 12     ~Test() { 13         cout << "Destructor is executed" << endl; 14  } 15 }; 16 
17 void myfunc() { 18     static Test obj; 19 } 20 
21 
22 int main() 23 { 24     cout << "main() starts" << endl; 25  myfunc(); 26     cout << "main() terminates" << endl; 27 
28     system("pause"); 29     return 0; 30 } 31 /* 輸出結果 32 main() starts 33 Constructor is executed 34 main() terminates 35 請按任意鍵繼續. . . 36 Destructor is executed 37 */
static obj in function

分析:執行函數時,首先調用構造函數,但函數結束並不會調用析構函數,由於此對象 是 static 並不會被銷燬,能夠看到,在主函數退出以後,執行了析構函數。

 1 #include <iostream>
 2 #include <string>
 3 #include <memory>
 4 
 5 using namespace std;  6 
 7 class Test {  8 public:  9  Test() { 10         cout << "Constructor is executed" << endl; 11  } 12     ~Test() { 13         cout << "Destructor is executed" << endl; 14  } 15 }; 16 
17 // 
18 void myfunc() { 19     static Test obj; 20 } 21 
22 
23 int main() 24 { 25     cout << "main() starts" << endl; 26 // myfunc(); // 並不調用函數 
27     cout << "main() terminates" << endl; 28 
29     system("pause"); 30     return 0; 31 } 32 /* 輸出結果 33 main() starts 34 main() terminates 35 請按任意鍵繼續. 36 */
not invoke function

分析:當不調用函數時,構造函數和析構函數都不會執行(有點奇怪。)

7,C++ 11 左值和右值

  https://blog.csdn.net/chy19911123/article/details/46013499

8,STL 中容器的線程安全問題

  https://stackoverflow.com/questions/5912539/stl-map-find-thread-safe

 9,爲何不要在構造函數/析構函數中調用虛函數?

  https://blog.csdn.net/xiaoqi2008/article/details/39371191

10,malloc設計的系統調用?

  https://blog.csdn.net/Always__/article/details/50990838

11,構造函數爲何不能是虛函數?

  https://blog.csdn.net/jiadebin890724/article/details/7951520

12,內存對齊以及如何關閉內存對齊?

  https://blog.csdn.net/a1205137569/article/details/48968699

13,不用 static 怎麼計數一個 const 函數被調用了幾回?(通常用 static,全局變量來進行計數)

  https://blog.csdn.net/sillyboard/article/details/594244(用了 mutable)

14,函數模板與類模板的區別?

  https://blog.csdn.net/tingzhushaohua/article/details/75331860

 15,C++構造函數參數列表與直接在構造函數中進行初始化有什麼區別?

  https://www.zhihu.com/question/60546182

 16,const 和 define 的區別?

  https://blog.csdn.net/yi_ming_he/article/details/70405364

 17,類裏面成員變量爲何不能用 memset() 來進行設置?會有什麼問題?

  https://blog.csdn.net/sulongvc/article/details/6064013

 18,什麼狀況下函數裏面的參數變量不同也能實現多態?

  還未獲得合理解答。

相關文章
相關標籤/搜索