In the operator overloaded function we returning *f i.e f is a pointer which stores the address of T type data and we are returning it by dereferencing it hence its return type should be of T, but in the Function definition we have written that T& should be returned but we are returning T.
Doubt in Operator overloaded function
T* search (string key){
int idx=hashfun(key);
Node<T>*temp=table[idx];
while(temp){
if(temp->key==key)return &temp->value;
temp=temp->next;
}
return NULL;
}
T& operator [](string key){
T* f=search(key);
if(f==NULL){
insert(key,0);
f=search(key);
}
return *f;
}
first take a look at search function
here we return address
this address is stored in f inside operator overloading function
hence f contains the address
f is pointer of type T
and it stores the address of a bucket (type T)
when we return *f means return value of f which is nothing but address
hence return type is T& (address of T)
i hope this helps
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.