-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_ptr.cpp
More file actions
51 lines (41 loc) · 1022 Bytes
/
unique_ptr.cpp
File metadata and controls
51 lines (41 loc) · 1022 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
#include <memory>
#include <iostream>
class Investment {
public:
virtual ~Investment() {}
};
class Stock : public Investment {
public:
};
class Bond : public Investment {
public:
};
class RealEstate : public Investment {
public:
};
void MakeLogEntry(Investment *pInvestment) {
// TODO: fill this function body
}
template<typename... Ts>
std::unique_ptr<Investment>
makeInvestment(Ts&&... params)
{
auto delete_investment = [](Investment *pInvestment) {
MakeLogEntry(pInvestment);
delete pInvestment;
};
std::unique_ptr<Investment, decltype(delete_investment)>
pInvestment(nullptr, delete_investment);
if (/*Stock*/1) {
pInvestment.reset(new Stock(std::forward<Ts>(params)...));
} else if (/*Bond*/1) {
pInvestment.reset(new Bond(std::forward<Ts>(params)...));
} else if (/*RealEstate*/1) {
pInvestment.reset(new RealEstate(std::forward<Ts>(params)...));
}
return pInvestment;
}
int main() {
std::unique_ptr<Bond> upbond = std::make_unique<Bond>();
return 0;
}