Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification on smart pointer's operator* and operator-> overloading

it's passed a lot since i used c++ so here the(probally dumb) question:

A basic smart pointer Object should behave like a normal pointer one, so in a typical implementation we add the * and -> operator to the object, something like this:

template <class T> class auto_ptr
{
    T* ptr;
    public:
    explicit auto_ptr(T* p = 0) : ptr(p) {}
    ~auto_ptr()                 {delete ptr;}
    T& operator*()              {return *ptr;}
    T* operator->()             {return ptr;}
   // ...
};

Now, in my knowings, the c++ * operator (dereference) stands for: "get the value pointed in the heap by the value of ptr" (is it right?), and the type of *ptr should be T. So why we would return an address?

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

Instead of:

T operator*()              {return *ptr;}

Second, by having the following snippet:

void foo()
{
    auto_ptr<MyClass> p(new MyClass);
    p->DoSomething();
}

Now, how can i access ptr->DoSomething() method by just writing p->DoSomething()? Logically i would come writing the wrong code:

p->->DoSomething();

Because p-> returns a T* and then i need another -> operator for access the DoSomething() method.

Thanks for any answer/clarification and sorry for eventually bad English.

like image 766
user3493193 Avatar asked Jan 10 '23 21:01

user3493193


1 Answers

In C++, when you evaluate a function, you end up with a value (unless the function's return type is void). The type of a value is always an object type. So when you say f(), that expression is a value of type T. However, there are different categories of value:

T    f();    =>   f() is a prvalue, passed along by copy
T &  f();    =>   f() is an lvalue, the same object that is bound to "return"
T && f();    =>   f() is an xvalue, the same object that is bound to "return"

So if you want a function to produce an existing value that you don't want to copy, you have to declare the return type of the function as one of the reference types. If the return type is not a reference type, then a copy of the return value will be made, and the caller only ever sees that copy.

like image 120
Kerrek SB Avatar answered Jan 21 '23 10:01

Kerrek SB