Created
October 10, 2017 19:38
-
-
Save rusdevops/f9a1b3511cd9f6924186dd272285da5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <utility> | |
template<typename T> | |
class pointer_t { | |
size_t size; | |
T* ptr; | |
public: | |
pointer_t() { | |
} | |
pointer_t(T * ptr_, size_t size_) : size{size_}, ptr{ ptr_ } { | |
} | |
~pointer_t() { | |
delete[] ptr; | |
} | |
pointer_t(const pointer_t<T>& other) : size{ other.size } { | |
if (other.ptr != nullptr) { | |
ptr = new T[other.size]; | |
std::copy(other.ptr, other.ptr + other.size, ptr); | |
} | |
} | |
pointer_t(pointer_t<T>&& other) : ptr{ other.ptr }, size{ other.size } { | |
other.ptr = nullptr; | |
other.size = 0; | |
} | |
void swap(pointer_t<T>& other) { | |
std::swap(this->ptr, other.ptr); | |
std::swap(this->size, other.size); | |
} | |
auto operator=(const pointer_t<T>& other) -> pointer_t<T>& { | |
if (this != &other) { | |
pointer_t{ other } .swap(*this); | |
} | |
return *this; | |
} | |
auto operator=(pointer_t<T>&& other) -> pointer_t<T>& { | |
if (this != &other) { | |
pointer_t{ std::move(other) } .swap(*this); | |
} | |
return *this; | |
} | |
friend auto operator<<(std::ostream& out, const pointer_t<T>& p) -> std::ostream& { | |
if (p.ptr == nullptr) { | |
out << "pointer: " << "empty"; | |
} else { | |
out << "pointer: " << p.ptr << " " << "{"; | |
for (auto i = 0; i < p.size; ++i) { | |
out << " " << *(p.ptr+i); | |
} | |
out << " " << "}"; | |
} | |
return out; | |
} | |
}; | |
int main () { | |
// emty constructor | |
pointer_t<int> ptr1; | |
std::cout << "pointer_t<int> ptr1;" << std::endl; | |
std::cout << "[ptr1] " << ptr1 << std::endl; | |
// constructor with parameter | |
pointer_t<int> ptr2{ new int[3]{3,4,5}, 3 }; | |
std::cout << "pointer_t<int> ptr2{ new int{3} };" << std::endl; | |
std::cout << "[ptr1] " << ptr1 << std::endl; | |
std::cout << "[ptr2] " << ptr2 << std::endl; | |
// copy constructor | |
pointer_t<int> ptr3 {ptr2}; | |
std::cout << "pointer_t<int> ptr3 {ptr2};" << std::endl; | |
std::cout << "[ptr1] " << ptr1 << std::endl; | |
std::cout << "[ptr2] " << ptr2 << std::endl; | |
std::cout << "[ptr3] " << ptr3 << std::endl; | |
// move constructor | |
pointer_t<int> ptr4 { std::move(ptr2) }; | |
std::cout << "pointer_t<int> ptr4 {ptr2};" << std::endl; | |
std::cout << "[ptr1] " << ptr1 << std::endl; | |
std::cout << "[ptr2] " << ptr2 << std::endl; | |
std::cout << "[ptr3] " << ptr3 << std::endl; | |
std::cout << "[ptr4] " << ptr4 << std::endl; | |
// assignment operator with copy semantics | |
ptr1 = ptr3; | |
std::cout << "ptr1 = ptr3;" << std::endl; | |
std::cout << "[ptr1] " << ptr1 << std::endl; | |
std::cout << "[ptr2] " << ptr2 << std::endl; | |
std::cout << "[ptr3] " << ptr3 << std::endl; | |
std::cout << "[ptr4] " << ptr4 << std::endl; | |
// assignment operator with copy semantics | |
ptr2 = std::move(ptr3); | |
std::cout << "ptr2 = std::move(ptr3);" << std::endl; | |
std::cout << "[ptr1] " << ptr1 << std::endl; | |
std::cout << "[ptr2] " << ptr2 << std::endl; | |
std::cout << "[ptr3] " << ptr3 << std::endl; | |
std::cout << "[ptr4] " << ptr4 << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment