c++ 面试题(C/C++/STL)

1,智能指针:auto_ptr(c++11 已经弃用),unique_ptr(用于取代 auto_ptr),  shared_ptr,  weak_ptr

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

 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/61008381

 3,C++11 新特性:

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

4,虚继承:

  http://www.cnblogs.com/heyonggang/archive/2013/08/13/3255155.html

5,C语言内存模型:

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

 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,

猜你喜欢

转载自www.cnblogs.com/zpcoding/p/10542470.html