Reference pointer

Possible duplicate:
Type of function argument followed by * &

I am looking at another user's code right now and saw an unusual (at least for me) syntax for declaring a function. Is the following C ++ syntax valid?

bool Foo::Bar(Frame *&ptoframe, int msToBlock) { .... } 

I think the developer is trying to declare a pointer to a link.

thanks for the help

+4
source share
3 answers

Yes it really is. Read the syntax from right to left: it reads a link to a pointer. This is important if you want to change the pointer passed to the function. The pointer is effectively passed by reference, like any other parameter.

Here is an example:

 void no_change(int * ptr) { ptr = 0; } void change_ptr(int *& ptr) { ptr = 0; } int main() { int *x; change_ptr(x); } 

The value of any pointer passed to change_ptr will be changed because we pass it by reference.

Please note that the value of the object pointed to by the pointer can be changed using the no_change function (i.e. *ptr = new int ). This syntax applies only to the actual pointer, and not to its object accessing the object.

+5
source

No, the first parameter of the function is a pointer to a pointer. Sometimes you want to change someone else’s pointer ... Wit:

 void change_my_char(char & c) { c = 'x'; } void pimp_my_pointer(void * & p) { p = nullptr; } int main() { char x; void * y; change_my_char(x); pimp_my_pointer(y); } 
+4
source

This is a pointer passed by reference. This allows the caller to change the value of the caller pointer.

+4
source

All Articles