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

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -