Friday, February 22, 2019

pointers - c++ * vs & in function declaration








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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...