Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ overloading dereference operators

Tags:

c++

I'm relatively new to C++, still trying to get a hang of the syntax. I've been taking a look at a few operator overloading examples, most recently smart pointer implementations. Here's a really generic example I'm looking at:

template < typename T > class SP {     private:     T*    pData; // Generic pointer to be stored     public:     SP(T* pValue) : pData(pValue)     {     }     ~SP()     {         delete pData;     }      T& operator* ()     {         return *pData;     }      T* operator-> ()     {         return pData;     } }; 

When overloading the dereference operator why is the type T&? Similarly, when overloading the structure dereference why is the type T*?

like image 751
user3273777 Avatar asked Feb 05 '14 05:02

user3273777


People also ask

Can dereference operator be overloaded?

The dereference operator ( * ) overload works like any other operator overload. If you want to be able to modify the dereferenced value, you need to return a non-const reference. This way *sp = value will actually modify the value pointed to by sp. pData and not a temporary value generated by the compiler.

What is the dereference operator in C?

In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, or l-value in memory pointed to by the variable's value. In the C programming language, the deference operator is denoted with an asterisk (*).

What is the difference between dereference operator and reference operator?

I read about * referencing operator and & dereferencing operator; or that referencing means making a pointer point to a variable and dereferencing is accessing the value of the variable that the pointer points to.

Can we overload this pointer?

There is no way to override operators on them. Pointers are (like char, int, float, etc...) a native type. You can create an object that behaves like a pointer and overload operators [->, *], but that will not overload [->, *] on the the native pointer.


2 Answers

The dereference operator (*) overload works like any other operator overload. If you want to be able to modify the dereferenced value, you need to return a non-const reference. This way *sp = value will actually modify the value pointed to by sp.pData and not a temporary value generated by the compiler.

The structure dereference operator (->) overload is a special case of operator overloading. The operator is actually invoked in a loop until a real pointer is returned, and then that real pointer is dereferenced. I guess this was just the only way they could think of to implement it and it turned out a bit hackish. It has some interesting properties, though. Suppose you had the following classes:

struct A {     int foo, bar; };  struct B {     A a;     A *operator->() { return &a; } };  struct C {     B b;     B operator->() { return b; } };  struct D {     C c;     C operator->() { return c; } }; 

If you had an object d of type D, calling d->bar would first call D::operator->(), then C::operator->(), and then B::operator->(), which finally returns a real pointer to struct A, and its bar member is dereferenced in the normal manner. Note that in the following:

struct E1 {     int foo, bar;     E1 operator->() { return *this; } }; 

Calling e->bar, where e is of type E1, produces an infinite loop. If you wanted to actually dereference e.bar, you would need to do this:

struct E2 {     int foo, bar;     E2 *operator->() { return this; } }; 

To summarize:

  1. When overloading the dereference operator, the type should be T& because that is necessary to modify the value pointed to by pData.
  2. When overloading the structure dereference, the type should be T* because this operator is a special case and that is just how it works.
like image 103
Matt Avatar answered Oct 07 '22 10:10

Matt


The purpose of dereference operator is to dereference a pointer and returns the object reference. Hence it must return the reference. That is why it is T&.

The purpose of referring operator (->) is to return a pointer and hence T* is returned.

like image 24
Saminathan S Avatar answered Oct 07 '22 11:10

Saminathan S