The difference between using refrences(&var), and pointers(*var) is that using refrences is more efficent because you do not have to derefernce the object. Am I right, and if I am why use pointers then?

Take this code for example:

#include<iostream>

//function prototype
void rFunc(int &rNum);
void pFunc(int *rNum);

//main method
int main()
{
    using std::cout;
    using std::endl;

    //SO all the variables w/ ref and pointers are initliazed
    int age = 5;
    int *pAge = &age;
    int &rAge = age;

    cout << "age: " <<  age << endl;
    cout << "*pAge: " <<  *pAge << endl;
    cout << "rAge: " <<  rAge << endl;
    cout << "using rFunc function..." << endl;

    rFunc(age); //Using refrence function
    cout << endl;

    cout << "now age: " <<  age << endl;
    cout << "now *pAge: " <<  *pAge << endl;
    cout << "now rAge: " <<  rAge << endl;

    cout << "using pFunc function..." << endl;
    pFunc(&age); //Using pointer function
    cout << endl;

    cout << "now age: " <<  age << endl;
    cout << "now *pAge: " <<  *pAge << endl;
    cout << "now rAge: " <<  rAge << endl;

    system("PAUSE");
    return 0;
}

void rFunc(int &rNum)
{
     using std::cout;
     using std::endl;

     cout << "Setting age to 15..." << endl;
     rNum = 15;

}

void pFunc(int *rNum)
{
     using std::cout;
     using std::endl;

     cout << "Setting age to 55" << endl;
     *rNum = 55;

}

Dani AI

Generated

Building on the replies from , and , here are a few practical points and modern-C++ alternatives that weren’t covered and are important for real code: lifetimes, member/rebindability issues, ownership, and move semantics.

Binding temporaries and dangling objects: binding a temporary to a const reference extends the temporary’s lifetime to match the reference’s scope. Conversely, returning a reference (or pointer) to a local automatic variable is undefined behavior — avoid it. Example of lifetime extension:

const std::string &sref = std::string("temp"); // temporary lives until sref goes out of scope

References as class members and containers: a reference member must be initialized (usually in the constructor initializer list) and cannot be reseated later, which makes assignment semantics awkward. If you need “reference-like” assignable behavior or to store references in a container, use std::reference_wrapper.

#include <functional>
#include <vector>

int a=1, b=2;
std::vector<std::reference_wrapper<int>> v{a, b};
v[0].get() = 10; // modifies a

Ownership and optional/array views: raw pointers give an address but don’t express ownership. Prefer std::unique_ptr/std::shared_ptr/std::weak_ptr for owning relationships, std::optional<T> when a value may be absent, and std::span (or string_view) for non-owning contiguous views. Example:

#include <memory>
auto p = std::make_unique<MyType>(); // sole ownership

Move semantics and forwarding: rvalue references (&&), std::move and std::forward let you transfer resources efficiently without copying. Use these tools rather than micro-optimizing pointer vs reference syntax. Cautions: watch object lifetimes, and pick the tool that expresses intent (ownership, optionality, aliasing) clearly.

Recommended Answers

All 2 Replies

That’s not really true. A reference is basically a pointer that the complier takes care of dereferencing for you. References also have limitations as that once they are referenced to and object they will always point to that object. Pointers can be change to point at different things at any time. When I code the only time I deal with actual pointers is when I am dealing with arrays. Otherwise I use references for function parameters if i need to be able to modify the variable. If I don’t need to modify the variable and it is a POD (plain old data) type then I will pass by value. If I am dealing with a non-POD type and I don’t need to modify the variable I will pass it by constant reference.

The difference between using refrences(&var), and pointers(*var) is that using refrences is more efficent because you do not have to derefernce the object.

While there's potential for such an optimization, I wouldn't rely on it. The benefit of references is largely syntactic convenience (for more than you probably expect).

Am I right, and if I am why use pointers then?

Pointers can point to nothing, references cannot. This may seem like a minor thing, but it's actually pretty big. Pointers can also be reassigned to point to another address while references are stuck. Changing the "thing" being aliased is pretty critical when using pointers for things such as linked data structures. And of course, dynamic memory specifically works with pointers, so at the very least you'd need to remove and add indirection to work with it as references (not recommended).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.