I have a method:
odp(foo& bar);
I’m trying to call it:
foo baz;
odp(&baz);
I get a compiler error:
error C2664: "odp" cannot convert parameter 1 from 'foo *' to 'foo &'
What am I doing wrong? Aren’t I passing in a reference to baz?
UPDATE: Perhaps I have a misconception about the relationship between pointers and references. I thought that they were the same, except references couldn’t be null. Is that incorrect?
When you apply the unary
&operator to an object, you get a pointer to that object, not a reference. You need a reference in this case. In order to initialize a reference parameter, you don’t need to apply any operators at all: an lvalue object can immediately be used by itself to initialize a reference of the appropriate type.The informal synonymity between the notions of “pointer” and “reference” (as in “pass by pointer” and “pass by reference”) is specific to C language. But in C++ they mean two completely different things.