Tuesday, 20 February 2018

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...