How do I use references?
A reference is merely an initialized pointer. This reduces signicantly zero pointer and
un-initialized pointers errors. Prefer references to pointers. Although the two following
codes look equivalent, the second implementation prevents invalid compilation.
// in C // in C++
int i; int i;
int *pi = &i; int &ti = i;
int *pj; int &rj; // g++ refuses to compile that
*pi = 0; ri = 0;
*pj = 0; // segfault rj = 0;
0 comments:
Post a Comment