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;

}

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.