From: Martin B. on
Razvan Cojocaru wrote:
>> Use a const pointer. Use it for the argument passing and if it's not too
>> much hassle also use it for the member.
>>
>> Bottom line for me is: Always use pointers for these parameters.
>> Use a pointer member in "90%" of cases.
>
> I don't see the difference between his problem using a non-const

He stated his problem as quote:"The problem is that the constructor
allows passing temporaries (...)".

> reference and what would ensue if he uses a const pointer (since a
> reference is pretty much the functional equivalent of a const pointer,
> dereferencing and checking for NULL aside).
>

Why did you snip the link I provided? It was discussed there.

The difference is that you can pass a temporary as a const-ref without
seeing it, while you cannot pass a temporary as a pointer without some
syntactic overhead. (This statement does *only* apply to the immediate
call - it does not apply to general lifetime management.)

Example:
A::A(int const& ref)
: m_ref(ref)
{ }

vs.

B::B(int const* ptr)
: m_ptr(ptr) // or m_ref(*ptr)
{ }

void f() {
int x;
char y;

A a1(x); // Fine, a1 can reference x from it's ref member
A a2(y); // The ctor get's passed a temporary int object!
// It does not reference y!
A a3(42); // compiles just fine, will reference a temp!

B b1(&x); // ok
B b2(&y); // good: compiler error, because char* <> int*
B b3(&42); // good: compiler error
}

Refs and Ptrs are functionally equivalent, I agree. They are absolutely
not treated equivalent by the compiler+lang rules though.

br,
Martin

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]