Click here to Skip to main content
15,036,363 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was learning about references in c++ and I realised that pointers could do the same thing:

with reference:
C++
int hello(int& hello)
{
    return hello;
}
int main()
{
    cout << "enter a num: ";
    int num;
    cin >> num;
    cout << num << endl;
    hello(num);
    cout << endl;
    cout << *num;
}
Outputs:
12

12

with pointer:
C++
int hello(int* hello)
{
    return *hello;
}
int main()
{
    cout << "enter a num: ";
    int *num = new int;
    cin >> *num;
    cout << *num << endl;
    hello(num);
    cout << endl;
    cout << *num;
}
Outputs:
12

12

My Question:
Since they both do the same thing(alias a variable), why do we use references over a pointer?

What I have tried:

i have tried looking it up, but cant find anything
Posted
Updated 5-Jul-21 2:45am
v2
Comments
Joe Woodbury 5-Jul-21 16:21pm
   
How does cout << *num; even compile?

A reference parameter tells the user that nullptr is invalid, whereas a pointer parameter might allow it.

A reference also allows you to write parameter.whatever, whereas the pointer requires an extra character: parameter->whatever. That's a big deal to some people! :)

But even though a reference says "I don't accept nullptr", it can still get one, so bulletproof code needs to include if(&ref != nullptr).

EDIT: Your code should include cout << hello(num) to better demonstrate your point. As it stands, hello(num) isn't doing anything because main ignores what it returns.
   
v4
A main difference is that pointers point normally to on the heap allocated memory (like structs or objects) and references mostly refer to variables from the stack. It becomes more important when bigger data chunks like object arrays, multimedia data or databases are processed.

Read this Stack vs Heap Memory Allocation to better understand this advanced topic. Some even more detailled video about this topic.
   
Mechanically, they are the same. There are two main usage difference though:

1) They cannot be NULL and they have to be passed in the code: any mistake is easily discovered at compile time.
2) You won't need to dereference them when assigning them values. That helps readability.

Also:
* You cannot inadvertantly modify the address they're pointing to;
* You don't have to / can't cast them, which helps being type safe.
   
Comments
Greg Utas 5-Jul-21 9:03am
   
A reference can most assuredly be nullptr. The invoker of a function can dereference a pointer and pass it to a reference argument: f(*p). If p is nullptr and the invoker never dereferenced p to access one of its fields, the crash occurs in f.
Pointers are more powerful than references. Unfortunately pointer usage is more prone to errors. So, the rule of thumb is: use a references everywhere you can. Use pointers where you cannot use references.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900