Monday 16 December 2019

pointers - c++ * vs & in function declaration





Possible Duplicate:
Difference between pointer variable and reference variable in C++





When should I declare my variables as pointers vs objects passed-by-reference? They compile to the same thing in assembly (at least run-time asymptotically) so when should I use which?



void foo(obj* param)
void foo(obj& param)

Answer



My rule is simple: use * when you want to show that value is optional and thus can be 0.



Excluding from the rule: all the _obj_s around are stored in containers and you don't want to make your code look ugly by using everywhere foo(*value); instead of foo(value);

So then to show that value can't be 0 put assert(value); at the function begin.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...