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.]
A
constpointer (T* const) can beNULL, 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 returnNULLif it didn’t find anything. You can’t do that if it returns a reference type.