question about returning pointers by reference
Jun 9, 2010 at 5:21am UTC
I have the class below,
1 2 3 4 5 6 7 8 9 10 11
class List{
public :
Node *pstart;
Node *& function(){
return pstart;
}
...
}
and I want to ban the assignation (in black), but at the same time -for speed reasons- I want to keep the return value as a reference.
1 2 3 4 5 6 7 8 9
int main(int argc, char ** argv){
List lista;
Node *listb=new Node;
lista.function()=listb;
return 0;
}
Is there any way I can achieve both requirements?
thanks in advance
santiagorf
Jun 9, 2010 at 5:43am UTC
Return a Node * const &. That is, a reference to a constant pointer to a Node.
Returning a reference doesn't give you any performance advantage over returning a pointer, by the way.
Jun 9, 2010 at 6:17am UTC
thank you!!. I was declaring the const before the *
Last edited on Jun 9, 2010 at 6:19am UTC
Topic archived. No new replies allowed.