References and Pointers

What are the differences between references and pointers?.

I am beginner in this field..so please do guidance to me
A pointer is a data type. A kind of object. Like an int, or a double, or a string, or anything else. A pointer is a kind of object.

A reference is another name for some object.

They're completely different concepts.


Internally, inside the final executable that your compiler makes, it may (or may not) implement them as very similar things. That's up to the compiler and it's not your problem; you should NOT think of them as similar things. They are very different concepts.

I'm stressing this because the fact that you're asking about the difference betwene them is a sign that perhaps you are already thinking they're the same kind of thing; they're really not.

Last edited on
Pointers and references are similar in many ways.

A reference is like a restricted pointer. You cannot reassign it, you cannot get the address of it (because it's not an object), and it cannot be null. The reference uses the exact same syntax as a regular object so it's very intuitive to use.

Pointers on the other hand can make the code more complicated, but if you need the extra functionality they might be the right choice.
Last edited on
"Pointers and references are similar in many ways."

I would take exception (a ha ha) to that. In what many ways are they similar?

Sure, the compiler might choose to use pointers to implement the concept of references, but that's up to the compiler.

In the C++ language, the only similarity of note (to my mind) is that they both get used as a way to reach some other object. That is an important similarity, true, but I don't think there are "many ways".
Last edited on
In what many ways are they similar?

If we didn't have references (like in C), we would use pointers to accomplish the same things.

Sure, the compiler might choose to use pointers to implement the concept of references, but that's up to the compiler.

The same could be said about pointers. In practice, a reference is very similar to a const pointer in the eyes of the compiler, except that a reference can be assumed to never be null.

That is an important similarity, true, but I don't think there are "many ways".

Well, maybe "many" is not the right word. What I mean is that they are very similar in what they can do and how they work.
Last edited on
we would use pointers to accomplish the same things.


Well, not everything. There are things each does that the other cannot do.

I think my philosophical objection to this is that teaching beginners to think of pointers and references as basically the same kind of thing is not helpful, and it can take a beginner years to drop that notion and start thinking about them differently.
I totally agree that they are different language features, each with its own set of language rules, but the use-cases overlap.

For example, if I want to avoid creating an unnecessary copy when an object is passed as argument to a function I would use a reference
1
2
3
void foo(const Bar&); // Requires a Bar as argument
Bar bar;
foo(bar); // A Bar is passed as argument 
But if I want it to be possible to call the same function without passing an object I would instead use a pointer (because it can be null).
1
2
3
4
void foo(const Bar*); // The Bar object is optional
Bar bar;
foo(&bar); // A (pointer to a) Bar is passed as argument
foo(nullptr); // no Bar is passed to the function 
Some might argue that that std::optional should be used in these situations but you cannot have an optional reference. I guess you could use std::optional<std::reference_wrapper<Foo>> but this becomes very verbose (and possibly less efficient) for something that could easily be handled using a pointer.

Another example is when you want to have a reference as member of a class. This has the not-so-obvious side effect that it makes the class non-assignable, because the reference can't be reassigned.
1
2
3
4
5
6
struct Ref { int& ref; };
int i;
Ref r1 {i};
int j;
Ref r2 {j};
r1 = r2; // error 
The solution is to use pointers.
1
2
3
4
5
6
struct Ref { int* ref; };
int i;
Ref r1 {&i};
int j;
Ref r2 {&j};
r1 = r2; // fine 


The guideline I follow is to always use references (because they make the code much cleaner and less error-prone) unless pointers are actually necessary for some reason.
Or, for that last example, a reference_wrapper these days.

I totally agree that they are different language features, each with its own set of language rules, but the use-cases overlap.

Frequently, yes. Saying they have overlapping use cases is much better than saying they're similar.
Last edited on
for that last example, a reference_wrapper these days.

I guess that could be a good idea. My only concern is that I now would have to sprinkle the code with calls to get().
Thank you all for this guidance
Topic archived. No new replies allowed.