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
| #include<iostream>
using namespace std;
template<typename T, class... Args>
class BaseVector
{
public:
virtual void pre_allocate(int capacity, Args...) = 0;
};
template<typename T, class... Args>
class SmallVector: public BaseVector<T, Args...>
{
public:
virtual void pre_allocate(int capacity, Args... args)
{
cout<<"small vector"<<endl;
p_ = (T*)malloc(sizeof(T) * capacity);
for (int i = 0; i < capacity; ++i) {
new(&p_[i]) T(std::forward<Args>(args)...);
}
}
private:
T *p_;
};
template<typename T, class... Args>
class FastVector: public BaseVector<T, Args...>
{
public:
virtual void pre_allocate(int capacity, Args... args)
{
cout<<"fast vector"<<endl;
p_ = (T*)malloc(sizeof(T) * capacity);
for (int i = 0; i < capacity; ++i) {
new(&p_[i]) T(std::forward<Args>(args)...);
}
}
private:
T *p_;
};
class TwoParameters
{
public:
TwoParameters(int a, int b)
{
//do something
}
};
class ThreeParameters
{
public:
ThreeParameters(int a, int b, double c)
{
//do something
}
};
int main()
{
SmallVector<TwoParameters, int , int> small_vec2;
SmallVector<ThreeParameters, int , int, double> small_vec3;
FastVector<TwoParameters, int , int> fast_vec2;
FastVector<ThreeParameters, int , int, double> fast_vec3;
////////////////////////////////////////////////////////
BaseVector<TwoParameters, int , int> *p = &small_vec2;
p->pre_allocate(1,1,1);
p = &fast_vec2;
p->pre_allocate(1,1,1);
//////////////
BaseVector<ThreeParameters, int , int, double> *q = &small_vec3;
q->pre_allocate(1,2,2,2);
q = &fast_vec3;
q->pre_allocate(1,2,2,2);
return 0;
}
|