Reference as function argument
Reference in fact store address of object, but has different syntax and feature
①We don’t need to worry null pointer dereferencing because there is no null reference(different feature)
②Although we use swap(a,b);我们真正传递的并不是a,b而是a,b的reference(different syntax)we can use a another way: void swap(int* x, int * y); swap(&a, &b);
Define parameter can use reference form or pointer form:
void swap(int& x, int & y) ;void swap(int* x, int * y);
Pass parameter can use reference form or pointer form:
swap(a, b); // reference from swap(&a, &b); // pointer form


浙公网安备 33010602011771号