Strange type in c++ -
i have method prototype:
bool getassignment(const query& query, assignment *&result); i bit confused type of second param (assignment *&result) since don't think have seen before. used like:
assignment *a; if (!getassignment(query, a)) return false; is reference pointer or other way around ? or neither ? explanation appreciated. thanks.
it's reference pointer. idea able change pointer. it's other type.
detailed explanation , example:
void f( char* p ) { p = new char[ 100 ]; } int main() { char* p_main = null; f( p_main ); return 0; } will not change p_main point allocated char array (it's definite memory leak). because copy pointer, it's passed value (it's passing int value; example void f( int x ) != void f( int& x ) ) .
so, if change f:
void f( char*& p ) now, pass p_main reference , change it. thus, not memory leak , after execution of f, p_main correctly point allocated memory.
p.s. same can done, using double pointer (as, example, c not have references):
void f( char** p ) { *p = new char[ 100 ]; } int main() { char* p_main = null; f( &p_main ); return 0; }
Comments
Post a Comment