parameter passing question

This is a discussion on parameter passing question within the C++ Programming forums, part of the General Programming Boards category; I'm new to C++ and have looked around for the answer to this seemingly simple question but can't seem to ...

parameter passing question

I'm new to C++ and have looked around for the answer to this seemingly simple question but can't seem to find it. Can someone please explain to me the difference between a function that looks like "funct( const type *& var )" and and one that looks like "funct( const type *var)" ?

I know the first one is a pass by constant reference, but I'm not sure what const means in the second one. I thought you needed the "&" to make it a pass by reference, so without what does that make the second one? I appreciate your answers.

One trick is to read the tokens from right to left. You see the &, so you know that a reference is involved. You see the *, so you know that it is a reference to a pointer. Then you see the const type, which can also be written as type const, and thus you know that it is a reference to a pointer to const type. Therefore, the object of the type cannot be changed through the pointer, but you can change the pointer, and a change to the pointer will be reflected in the caller due to it being a reference parameter.

For the second one, it is the same thing, except that it is not a reference parameter, so if you assign to the pointer, only the local pointer is changed.

If you actually wanted a const reference to a pointer to non-const, you would write it as funct( type* const& var ). Now, reading the tokens from right to left, you see that the const applies to the pointer.

thanks, that helped a lot. So does the const in the second one even do anything? if it's just passing the pointer by value (making the local variable a copy of the pointer being passed), you don't have access to the type (which is const) that the pointer is pointing to to change it in the first place, right?

is the const just there as a formality, meaning that whatever pointer variable is passed into this function, it MUST be a pointer to a const type (even tho the pointer is just going to be copied)? and if that's the case, then does that mean the local variable (which is a pointer) can be changed to point to ONLY types that are const?

thanks, that helped a lot. So does the const in the second one even do anything? if it's just passing the pointer by value (making the local variable a copy of the pointer being passed), you don't have access to the type (which is const) that the pointer is pointing to to change it in the first place, right?

wrong

it does copy the pointer. but the memory it points to - is still the same as in the calling function

const tell the compiler that the function does not intend to change the contents of the memory that is pointed by the pointer

I don't fully understand what you are saying, so let's sum it up:
const T* -> pointer to const T. Hence if you dereference the pointer, you get const T, which cannot be assigned, only read. Of course, you can also assign it a different address, which is then implicitly converted to const T*. So there is an idea behind taking a parameter to const - it makes sure the function cannot modify what it points to.
Any T -> const T is implicit. That is, you can pass in any variable of type T to a function that expects a const T. However, the reverse is not true.
T* const -> a constant pointer to T. Hence you cannot change the value of the actual pointer, but you can change what it points to.