-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_ptr.h
More file actions
38 lines (38 loc) · 814 Bytes
/
unique_ptr.h
File metadata and controls
38 lines (38 loc) · 814 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
#ifndef UNIQUE_PTR
#define UNIQUE_PTR
#include <algorithm>
#include <iostream>
template<typename T>
class unique_ptr{
T* ptr;
public:
unique_ptr(T* dat=nullptr):ptr(dat){
}
unique_ptr(unique_ptr<T> &&src)noexcept{
ptr=src.ptr;
src.ptr=nullptr;
}
~unique_ptr()noexcept{
delete ptr;
}
unique_ptr<T>& operator =(unique_ptr<T> &&src)noexcept{
std::swap(ptr,src.ptr);
return *this;
}
T* operator ->()const{
return ptr;
}
T& operator *()const{
return *ptr;
}
bool operator !()const{
return ptr==nullptr;
}
explicit operator bool()const{
return ptr!=nullptr;
}
private:
unique_ptr(unique_ptr<T> &src)=delete;
unique_ptr<T>& operator =(unique_ptr<T> &src)=delete;
};
#endif