-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrefCount.cpp
More file actions
55 lines (35 loc) · 784 Bytes
/
refCount.cpp
File metadata and controls
55 lines (35 loc) · 784 Bytes
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
/*
* HOME : ecourse.co.kr
* EMAIL : smkang @ codenuri.co.kr
* COURSENAME : C++ Template Programming
* MODULE : refCount.cpp
* Copyright (C) 2017 CODENURI Inc. All rights reserved.
*/
#include <iostream>
using namespace std;
class RefCountBase
{
protected:
mutable int mCount;
~RefCountBase() { }
RefCountBase() : mCount(0) {}
public:
void addRef() const { ++mCount; }
};
template<typename T> class RefCount : public RefCountBase
{
public:
void release() const
{
if (--mCount == 0)
delete static_cast<const T*>(this);
}
};
class Truck : public RefCount<Truck> {};
class Bus : public RefCount<Bus > {};
int main()
{
const Truck* p = new Truck;
p->addRef();
p->release();
}