Answer the question
In order to leave comments, you need to log in
Can a constant pointer be considered analogous to a reference in C++?
Hello!
For example, a constant pointer: *const Cat
and a reference: &Cat
Can these ways of accessing an object be considered similar and interchangeable?
Answer the question
In order to leave comments, you need to log in
No, but the difference is more significant than the ability to assign nullptr. A constant reference can extend the lifetime of a temporary object, but a pointer cannot.
For example, you can:
T f();
...
const T& obj = f();
obj.foo();
T f();
...
const T* const obj = &(f());
obj->foo();
Нет. В указатель можно засунуть nullptr, а в ссылку - нет.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question