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,什么状况下函数里面的参数变量不同也能实现多态?

  还未获得合理解答。

相关文章
相关标签/搜索