Hi, I was faced with this exercise where i have to create my own
unique_ptr
so i had to overload operators
->
,
*
I found some stuff on stackoverflow and it really helped me understand
operator*()
But not much made sence about
operator->
because looked like there were 3 kind of ways and every way is different
operator->
& operator->
* operator->
http://stackoverflow.com/questions/8777845/overloading-member-access-operators-c
Also tried to search for this in
http://www.cplusplus.com/reference/ but nothing.
So my question is could somebody explain how these 3 ways of overloading this opperators differ?
Also - what book should i buy to get the most complete reference book available for any price that would also include stuff like this?
My code so far without
->
1 2 3 4 5 6 7 8 9
|
template<class T>
class unique_ptr{
T* ptr;
public:
unique_ptr(T* p) : ptr(p){}
~unique_ptr(){ delete ptr; }
T& operator*(){ return *ptr; }
void release() { delete ptr; }
};
|
This reminds me of 1 extra question :
What if in my code i allocate space for more than one element when defining
unique_ptr
for example
unique_ptr<int> p{ new int[5]{1, 2, 3, 4, 5} };
In my destructor
delete
is used to release memory but as far as i know on arrays I have to use
delete[]
. So maybe i can use 1 of them in both cases? If not how can I handle this?
Also if there is any book(s) that comes in mind that helped you understand this stuff yourself you are welcome to share the titles of those books :)