-
Notifications
You must be signed in to change notification settings - Fork 1
/
item18_unique_ptr.cpp
83 lines (76 loc) · 2.19 KB
/
item18_unique_ptr.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
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <memory>
#include "type_name.hpp"
using namespace std;
class Investment{
public:
int k;
Investment(int _m):m(_m){};
virtual ~Investment(){};
private:
int m;
};
class Stock: public Investment {
public:
Stock(int _m=0):Investment(_m){};
//~Stock(){};
};
class Bond: public Investment {
public:
Bond(int _m=0):Investment(_m){};
//~Bond(){};
};
class RealEstate: public Investment{
public:
RealEstate(int _m=0): Investment(_m){};
//~RealEstate(){};
};
/* template return unique_ptr pointer */
template<typename... Ts>
std::unique_ptr<Investment> makeInvestment(Ts&&... parameters){
cout << "Use simple makeInvestment \n";
return make_unique<Stock>(std::forward<Ts>(parameters)...);
}
auto delInvmt = [](Investment* pInvestment){
cout << "delete the pointer from lambda function";
cout << "the value pInvestment pointed by is : " << pInvestment->k << endl;
delete pInvestment;
};
/* Textbook example */
/* self defined delete function. unique_ptr support self */
template<typename... Ts>
/* Maybe can use auto */
//std::unique_ptr<Investment, decltype(delInvmt)> makeInvestment(int type_code, Ts&&... params){
auto makeInvestment(int type_code, Ts&&... params){
cout << "Use complex makeInvestment \n";
std::unique_ptr<Investment, decltype(delInvmt)> pInv(nullptr, delInvmt);
// Use type code to determine which investment.
if (type_code == 1){
pInv.reset(new Stock(std::forward<Ts>(params)...));
}else
{
pInv.reset(new Bond(std::forward<Ts>(params)...));
}
return pInv;
}
/* Example add all variables together */
template<typename T>
T adder(T v){
return v;
}
template<typename T, typename... Targs>
T adder(T first, Targs... Fargs){
return first + adder(Fargs...);
}
int main(){
Stock* s = new Stock(10);
Investment* i= s;
long sum = adder(1,2,3,4);
cout << "Sum of 1,2,3,4 : " << sum << endl;
auto pInvestment = makeInvestment(2, 3);
auto pInvestment1 = makeInvestment(1);
auto pInvestment2 = makeInvestment();
auto pInvestment3 = makeInvestment(2.0);
cout << "Type of pInvestment : " << type_name<decltype(pInvestment)>() << endl;
return 0;
}