In addition to Russco's explanation, if you wanted the C equivalent of
then you would need to writeCode:void set(int*& ptr) { ptr = assign_to_something(); }
which not only makes the function look slightly messier, but from an interface perspective, it is not obvious why you require int** ptr - a user may legitimately ask, is it because you want to modify the pointer or, is it that you require int** prt because you are expecting a pointer to an array of pointers? In addition, the caller needs to reference the pointer in the function call i.e.Code:void set( int** ptr) { *ptr = assign_to_something(); }
The pass-by-referece mechanism that C++ offers is much clearer and leaves far less room for error:Code:int main() { int* ptr=0; set(&ptr); }
Code:void set(int*& ptr) { ptr = assign_to_something(); } int main() { int* ptr=0; set(ptr); }




Reply With Quote
