Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to assume that a reference is passed as a pointer

I have a function defined like this:

void doSomethingWithCustomer (const Customer &customer);

One of my fellow developers called it like this:

Customer *customer = order.getCustomer();
doSomethingWithCustomer (*customer);

Unfortunately, the getCustomer method can return a nullptr, if the order is not tied to a customer. If getCustomer returns a nullptr, then the application does not crash at the time of the call to doSomethingWithCustomer but rather within the function, where the customer reference is used.

Of course the correct way to write this is to check for customer not being a nullptr first, then call the function if we have a valid customer. Normally we expect that if a function/method has a reference argument, that the caller checks the validity of it (which wasn't the case here), instead of the function itself checking the argument.

I know that Visual Studio 2010 (and earlier versions) passes references by actually passing the pointer, but I wonder if this is indicated somewhere in the C++ standard. Can we assume that a reference is always passed as a pointer (personally, I wouldn't rely on this, but it's interesting to know it)?

Is it possible to tell Visual Studio that when passing a reference, it should automatically dereference it first and crash at the time of the call rather then somewhere much deeper (doing this in a debug version might be sufficient)?

like image 889
Patrick Avatar asked Nov 20 '12 09:11

Patrick


People also ask

Can a reference be passed as a pointer?

Passing Reference to a Pointer in C++ Note: It is allowed to use “pointer to pointer” in both C and C++, but we can use “Reference to pointer” only in C++. If a pointer is passed to a function as a parameter and tried to be modified then the changes made to the pointer does not reflects back outside that function.

What does it mean to pass a pointer by reference?

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.

Are pointers passed by reference in C?

In C, pass by reference is emulated by passing a pointer to the desired type. That means if you have an int * that you want to pass to a function that can be modified (i.e. a change to the int * is visible in the caller), then the function should accept an int ** .

When must you use a pointer rather than a reference?

Use references when you can, and pointers when you have to. References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.


2 Answers

Is it valid to assume that a reference is passed as a pointer?

No, it is not.
The standard does not mandate that the reference should be implemented in terms of pointer.

How to actually implement a reference is an implementation detail which the standard leaves out for implementations to decide on. It only describes the expected behavior from an Reference and one of them is a reference can never be NULL in a standard conformant program.

If your function parameter is expected to be NULL sometimes then you should pass it as an pointer.

like image 88
Alok Save Avatar answered Oct 13 '22 00:10

Alok Save


No. It's completely implementation defined.

For diagnostic purposes, I've created a little container type which validates the parameter. You would then declare the function/method:

void doSomethingWithCustomer(const t_nonnull<const Customer>& pCustomer);

where the t_nonnull type validated the parameter at construction. However, I've found it more useful to just use references more and more frequently (IOW, don't return a pointer in this case -- just consider it an error to access the customer when the customer does not exist).

like image 25
justin Avatar answered Oct 13 '22 00:10

justin