tags:

views:

120

answers:

6

Say I have a function that takes a const reference to a pointer...

Example:

void Foo( const Bar *&p_Thing, );

and I pass a pointer

Bar *blah = NULL; // Initialized when program starts up

to the function

Foo( blah );

I may encounter a compiler error like this

invalid initialization of reference of type 'const Bar*&' from expression of type 'Bar*'

This has happened to me a few times, and I'd really like to clear up how const operates in terms of applying to parameters in relation to argument passing. Any help is appreciated, thanks.

A: 

I assume you want a const reference to a Bar*, not a reference to a const Bar*? If yes you want to define it as void Foo(Bar* const& p)

tyranid
A: 

I could never figure the syntactic difference between pointers to references and references to pointers. So I disambiguate:

typedef const Bar *PBar;
void Foo(const PBar &p_Thing);

This way you also get rid of the "const pointer"/"pointer to const" ambiguity.

Seva Alekseyev
+2  A: 

This is what you want:

void Foo( Bar * const &p_Thing );

Then it becomes a const-reference to a Bar * pointer, which has the lovely feature of compiling.

scotchi
Thanks. This and Matthew's comment helped me understand the problem, and the fix.
Anonymous
A: 

The response in this particular case is easy: the operation is expecting a reference to a pointer to const memory, but you're passing it a pointer to a non-const memory. Making blah a const Bar* would allow it.

However, you should consider this passing scheme if you're having trouble with pointers and constness. Passing a pointer by reference is not uncommon, but for 90% of the tasks it is not really necessary. You could pass the pointer, or a reference, or return it as a value of the function or method call, instead. That would be easier in terms of memory management and legibility of your code too.

Diego Sevilla
A: 

Your function is requiring a pointer to constant data.

You are passing a pointer to mutable or non-constant data. Some compilers may complain about the constness of the target data.

Thomas Matthews
A: 

While most questions do answer the question (you cannot pass a mutating pointer by reference into a function that takes a non-mutating pointer by reference), I don't think the rationale is made explicit in any of the answers:

It would break const-correctness. If you pass a non-mutating pointer by reference to a function taking a constant pointer by reference, it could well modify the pointer to refer to a constant object (after all, the type guarantees that it will not modify that value). Now the problem is that outside of your function the pointer passed in is actually not const, and the compiler would have to allow any mutating operation on the pointed object, and that would imply modifying an object that was initially constant.

Here is a short example:

const int y = 0;
void f( const int *& p ) {
   p = &y; // ok, p is a pointer to a constant integer
}
int main()
{
   int *p;
   f( p ); // if this was allowed p would point to y, but y is constant
   *p = 5; // correct, p is a mutating pointer
}
David Rodríguez - dribeas