Few operator overloading that bothers me

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 :)
Last edited on
For example here I made my counted_ptr class
How can i make the last line to work (so this question becomes more specific)
cout << p2->get() <<endl;

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
#include <iostream>
using namespace std;


template<class T>
class counted_ptr{
	T* ptr;
	int* count;
	void increase(){ (*count)++; }
	void decrease(){ (*count)--; }
public:
	counted_ptr(T* t) : ptr(t), count(new int{1}){}  //constructor
	counted_ptr(const counted_ptr& p);               //copy constructor

	T& operator*() { return *ptr; }
	const T& operator*() const { return *ptr; }

	~counted_ptr();
	int get() const { return *count; }
};

template<class T>
counted_ptr<T>::counted_ptr(const counted_ptr<T>& p) : ptr{ p.ptr }, count{ p.count }{
	increase();
}

template<class T>
counted_ptr<T>::~counted_ptr(){
	decrease();
	if (!(*count)) delete ptr;
}

int main(){

	counted_ptr<int> p{ new int{ 9 } };
	counted_ptr<double>* p2 = { new counted_ptr < double > { new double{ 2 } } };
	
	cout << p2->get() <<endl;              // how to make this work?
}
Last edited on
cout << p2->get() <<endl; // how to make this work?

This already works and it calls the counted_ptr<double>::get() function.

I think this is closer to what you are asking for:
1
2
3
4
5
6
7
struct A {
	void print() { std::cout << "hello" << std::endl;}
};
int main() {
	counted_ptr<A> p{ new A };
	p->print(); // You want this to call A::print()
}

To make this work you have to operload operator->. The return value should be a pointer to the object that the member on the right side belongs to. In other words, you want operator-> to return ptr.
Last edited on
Cant believe i missed that it worked. I thought i tried it and it didn't work but yea it actually does :D
Thank you :)
Topic archived. No new replies allowed.