operatiors

closed account (jGAShbRD)
Dereference operator * that returns a reference to the item pointed by the object.
Operator -> that returns the pointer to the object. It allows to use the my_unique_ptr object for class member access.

would i write operatir -> as
Type* operator->(); or Type* operator-> () const; why?
how do i write derefernce?
Why would it be a pointer? Your operator implies that you are going into the pointer to get the thing itself, so wouldn't it just return type, not type*?
The operator-> can be marked const if you're doing a re-implementation of a smart-pointer-esque object, because the function doesn't need to modify the pointer itself.

jonnin, I think that's just how it works with operator overloading. Kinda like how post-increment ++ has special rules, the -> operator also has special rule in that it automatically dereferences the returned object to access its member.
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
// Example program
#include <iostream>
using std::cout;

struct Thing {
    void foo() { cout << "Hello\n"; }
};

struct Holder {
    Holder(Thing* thing)
    : pThing(thing) { }
    
    Thing* operator->() const
    {
        return pThing;
    }
    
    Thing* pThing;
};

int main()
{
    Thing thing;
    Holder holder(&thing);
    holder->foo();
}


Edit: Also,
https://en.cppreference.com/w/cpp/language/operators
cppreference wrote:
The overload of operator -> must either return a raw pointer, or return an object (by reference or by value) for which operator -> is in turn overloaded.
Last edited on
closed account (jGAShbRD)
this is all in my public for a class with template
closed account (jGAShbRD)
template <typename Type>
class my_unique_ptr{
public:
my_unique_ptr();//default constructor unique pointer
my_unique_ptr(Type* target);//takes Type as parameter unique pointer
my_unique_ptr(my_unique_ptr<Type> &&target);//move constructor unique pointer
my_unique_ptr(const my_unique_ptr<Type> &target)=delete;//disable copy constructor unique pointer
~my_unique_ptr();//destructor unique pointer
Type& operator*() const;//dereference pointer unique pointer ???????? const or no?
Type* operator->() const;//operator-> unique pointer ????????? const or no?
my_unique_ptr<Type>& operator=(my_unique_ptr<Type> &&target);//move assignment unique pointer ?????? would this be my_unique_ptr & operator or does it have to have <type>
my_unique_ptr<Type>& operator=(const my_unique_ptr<Type> &target)=delete;//disable copy assignment unique pointer
bool isNullptr();//check state unique pointer


private:
Type* raw_pointer;

};
closed account (jGAShbRD)
not sure if it is like that ^
Right, the -> and * operators can be marked as const, as they don't need to change the unique_ptr object itself. Your operator= signature also looks OK as far as I can tell, but I don't know the details of any pitfalls or "gotchas" of implementing a smart ptr, so my advice would just be to try it out and experiment.
closed account (jGAShbRD)
also
my_unique_ptr<Type>& operator=(my_unique_ptr<Type> &&target) is this how u write it
or is it
my_unique_ptr<Type>& operator=(my_unique_ptr && target)
or
my_unique_ptr & operator=(my_unique_ptr && target)

also for pointers is it like typename * ptr or does it have to be typename* ptr? or like operator * or operator* does spacing matter?
my_unique_ptr is a class template, so my_unique_ptr isn't a class in itself. Yes, you need my_unique_ptr<Type> in every case.

Spacing does not matter for the operator*, it's just a personal preference.
Last edited on
Topic archived. No new replies allowed.