c++ - putting a '&' after the type -


i new programming. moving on c++ c in college courses, , encountered haven't seen before in c. after type, either in function declaration or passing parameter, & follows type. example, use struct called customer in 1 of our projects, , of functions pass customer&. why ampersand after type, opposed in front? thanks!

references in c++ allow cleaner way execute following code:

int x = 16; int* y = &x; cout << *y; 

which written instead as

int x = 16; int& y = x; cout << y; 

when defining functions, reference allows function change value of parameters without causing user of function put ampersand before everything. e.g.

void func( int& ) {     = 5; }  void main() {     int = 10;     func( );     cout << a; // output '5' } 

be careful this, programmer using functions without checking implementation might not realize function changing value of parameters unless intent obvious. it's better use pointers or return statement instead of using references this.

for cases, see references used easy way make alias.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -