16.25
extern template class vector<string>;
实例化声明,表示其他地方有实例化
template class vector<Sales_data>;
实例化定义
16.26
16.27
a.实例化了char类型的class
b.实例化了double的类型的class
c.实例化了int的类型的class
d.实例化了char类型的class
e.对sc进行调用,未进行实例化
f.实例化了string类型的class
16.28
template <typename T, typename Del>
class my_shared_ptr {
public:
my_shared_ptr() :data(nullptr), useCount(new int(1)) {
}
my_shared_ptr(T* value):data(value), useCount(new int(1)){
}
my_shared_ptr(const my_shared_ptr& msp):data(msp.data),use_count(msp.useCount){
if(*useCount){
++*useCount;
}
}
my_shared_ptr& operator=(const my_shared_ptr& msp){
if(--*useCount == 0){
delete data;
delete useCount;
}
data = msp.data;
useCount = msp.useCount;
++*useCount;
return *this;
}
size_t use_count(){
return use_count;
}
T* get() {
return data;
}
void swap(my_shared_ptr& msp){
std::swap(this->data, msp.data);
}
void reset(my_shared_ptr& data){
if (--*use_count == 0) {
delete data;
delete use_count;
}
this->data = data.data;
this->useCount = data.useCount;
++*data.use_count;
}
bool unique(){
if(*useCount == 1){
return true;
}
return false;
}
T& operator*() { return *p; }
T& operator*() const { return *p; }
~my_shared_ptr(){
if(--*useCount){
Del ? Del(data) : delete data;
}
}
private:
T* data;
size_t* useCount;
Del d = 0;
};