Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing between reference (T&) and const pointer (T* const)

Is there any reasonable use case, where one should use const pointer over reference?

T obj;
T &r = obj;  // style-1
T* const p = &obj; // style-2

Both the style can be used for the same purpose. I always prefer the 1st style in the code and consider the later style as deprecated. However I still wonder if missed any use case where 2nd style is better ?

Edit: Not limiting to the above example, I talk in a broader sense,

void foo (T& r); // style-1
void foo (T* const p); // style-2

[I see from few of the answers that, style-2 allows to pass null.]

like image 355
iammilind Avatar asked Sep 29 '11 18:09

iammilind


People also ask

What kind of references should I use?

They can be friends, a landlord, clients or anyone who can speak to your good personal qualities such as your honesty, dependability, good nature, etc. If they have firsthand knowledge of your work skills, that is even more useful.

Who should your references be?

They're often friends, coworkers or college instructors. While there may be many options within your life, choose your references carefully. Friends or coworkers who are more likely to speak favorably of you are the best options. Related: Personal vs.

Do I need 2 or 3 references?

Three is the minimum number of references to include on your job application. The employer may be able to form a thorough impression of you as a candidate after listening to diverse perspectives. However, the number of references required may depend on the role and the company.


2 Answers

A const pointer (T* const) can be NULL, and that can be useful when passing arguments to a function or when returning values from a function. For example, if you have a search function, you might want it to return NULL if it didn't find anything. You can't do that if it returns a reference type.

like image 88
Idan Arye Avatar answered Sep 26 '22 03:09

Idan Arye


Let me go out on a limb here. Since you explicitly say "const pointer", I'm assuming that you are not talking about function arguments or even function return values. For a function argument passed by copy, the constness is an irrelevant implementation detail:

void foo(T *);          // declaration

void foo(T * const pt)  // implementation,
{ /* ... */ }           // pt is const inside the body (who cares?)

Therefore, the only use case that comes to mind is if you need to make an alias somewhere inside your own code. In that case I'd always prefer the reference. Compare:

for (auto it = box.begin(); it != box.end(); ++it)
{
  T & trinket = *it;     // nice
  T * const ptr = &*it;  // wtpf?

  // ...
}

Since you edited your question: There is obviously a difference for function arguments.

void foo(T &);
void bar(T *);

In foo you are guaranteed to have a workable, mutable object reference; in bar you have to check whether the pointer is not null (giving you the notion of an optional argument). In that sense, T& and T* aren't really comparable.

like image 28
Kerrek SB Avatar answered Sep 23 '22 03:09

Kerrek SB