Search code examples
c++pointersparametersreferenceconceptual

Is passing by reference is a special case of passing as pointer?


I haven't understand passing by reference in C++ completely. I already read related questions like the following ones.

But even though I understand that it is different to a pointer, I don't fully understand the concept of reference in C++. How does it work? I seems like reference is a special case of a pointer and might be implemented as such by the compiler. But it behaves different on programming level. Is that correct?

Is the concept of passing by reference a special case of passing a pointer to that value? Many times I have to decide whether to use reference or pointer while programming. Moreover I want to understand the underlying principle. Is only the memory address copied?


Solution

  • yes, it's essentially the same as a pointer but a nicer and safer version as the pointer address cant be changed or set to null. i see it mainly as a means to have nice function interfaces without requiring the user of the interface to worry about pointers - ie function remain simple yet the object is not copied, purely its address. class copy constructor is a prime example of where references are crucial.