Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot assign to overloaded dereference (*) operator

I have overloaded dereference operator for my template class:

 template <class T> class Node {

 public:
     T *pointer;
     T operator*() { return *pointer; }
 };

I want to be able to write to the pointer in main:

Node<int> n;
*n = 33;

But I get this error:

lvalue required as left operand of assignment

How should I overload this operator to be able to write to the pointer?

like image 789
maciejjo Avatar asked Dec 25 '22 16:12

maciejjo


1 Answers

Just give it T& as the return type. Then you have an lvalue. The problem right now is that you're returning a copy of the object pointed to.

like image 116
Sebastian Redl Avatar answered Dec 28 '22 07:12

Sebastian Redl