-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path面试题48之不能被继承的类_SealedClass.cpp
71 lines (59 loc) · 1.1 KB
/
面试题48之不能被继承的类_SealedClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SealedClass.cpp : Defines the entry point for the console application.
//
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛
#include "stdafx.h"
// ====================方法一====================
class SealedClass1
{
public:
static SealedClass1* GetInstance()
{
return new SealedClass1();
}
static void DeleteInstance( SealedClass1* pInstance)
{
delete pInstance;
}
private:
SealedClass1() {}
~SealedClass1() {}
};
// 如果试图从SealedClass1继承出新的类型,
// 将会导致编译错误。
/*
class Try1 : public SealedClass1
{
public:
Try1() {}
~Try1() {}
};
*/
// ====================方法二====================
template <typename T> class MakeSealed
{
friend T;
private:
MakeSealed() {}
~MakeSealed() {}
};
class SealedClass2 : virtual public MakeSealed<SealedClass2>
{
public:
SealedClass2() {}
~SealedClass2() {}
};
// 如果试图从SealedClass1继承出新的类型,
// 将会导致编译错误。
/*
class Try2 : public SealedClass2
{
public:
Try2() {}
~Try2() {}
};
*/
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}