Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an & in C++

Tags:

c++

I was wondering how using an & to access a certain memory location changed the nature of a function call. For example, if I had written a function to set the radius of a circle

//True, if success, False if radius is off-screen
bool SetRadiusCircle(Circle &b, int r)

This was an example given in the assignment my professor gave me. I just wanted to know how the "&" he included in the sample function call differed from simply using Circle b.

like image 637
classISover Avatar asked Nov 16 '11 15:11

classISover


People also ask

WHEN TO USE a or an?

"A" goes before words that begin with consonants. "An" goes before words that begin with vowels: an apricot. an egg.

What is the rule for using an?

The general rule is to use a when the indefinite article precedes a word beginning with a consonant sound and an when it precedes a word starting with a vowel sound.

What is example of using a and an?

We call the the definite article and a/an the indefinite article. For example, if I say, "Let's read the book," I mean a specific book. If I say, "Let's read a book," I mean any book rather than a specific book.


2 Answers

Yes. If you use &, you are passing the circle as a reference. If you don't use it, you are passing a copy of the circle object, copy that gets created by the compiler for you. This triggers the class copy constructor (either the one you defined, or the default one, which is a bitwise copy).

With a reference, you are not copying the object. You are instead getting the object you had in the caller. It's equivalent of having an alias to the original object, and any change you perform will apply to the object passed. In the case of a copy, any change to the object is lost at the end of the call, because the copy is destroyed.

When you use a reference, the internal use of that argument requires you to use the . to access that object's members (e.g. b.getRadius()). If you defined your function to accept a pointer instead (func (Circle *bPtr)) then you must use the -> (e.g. bPtr->getRadius()). Using a pointer is different from using a reference, but the final, practical effect is the same: you get to manipulate the original object, not a copy.

Note that this is valid in the function definition. An & used in another context gives you the pointer where something resides. They are not the same, although they use the same symbol.

like image 56
Stefano Borini Avatar answered Sep 21 '22 15:09

Stefano Borini


The & here means a Reference.
It is just an alias to the variable being passed.

Any modification performed on b inside the function will result in modification of the Object being passed to the function.

Without the & an copy of the object will be passed to the function and any modification performed on it inside the function will not modify the Original Object.

Detailed explanation:

Consider the simple example below, it demonstrates the 3 ways in which you can pass arguments to a function:

Pass By value:

void passByValue(MyClass obj)

When an function argument is passed by value, the function receives an copy of the variable being passed, any modification done on this copy inside the function body does not affect the original variable being passed.

Pass an Reference:

void passAnRef(MyClass &ref)

The function receives an alias to the variable being passed.Any modification performed on this alias modify's the original variable as well. If the argument being passed is an custom class object then you can access the class members just like accesing through an object by using the . operator.

Pass an pointer:

void passAnPtr(MyClass *ptr)

The function receives an pointer to the original variable being passed. i.e: It receives an variable which points to address of original variable in memory. Any modification performed through this pointer modify's the original object since it just points to that memory.
If the argument being passed is an custom class object then you can access the class members just like accessing through an pointer to object by using the -> operator.

Online Demo:

#include<iostream>
using namespace std;

class MyClass
{
    public:
         int i;
         MyClass():i(0){}
};

void passByValue(MyClass obj)
{
    obj.i = obj.i + 10;

}
void passAnRef(MyClass &ref)
{
    ref.i = ref.i + 10;
}
void passAnPtr(MyClass *ptr)
{
    ptr->i = ptr->i + 10;
}

int main()
{
    MyClass obj;

    cout<<"Before passByValue obj is:"<<obj.i;
    passByValue(obj);
    cout<<"\nAfter passByValue obj is:"<<obj.i;

    cout<<"\nBefore passAnRef obj is:"<<obj.i;
    passAnRef(obj);
    cout<<"\nAfter passAnRef obj is:"<<obj.i;

    cout<<"\nBefore passAnPtr obj is:"<<obj.i;
    passAnPtr(&obj);
    cout<<"\nAfter passAnPtr obj is:"<<obj.i;

    return 0;
}

Output:

Before passByValue obj is:0
After passByValue obj is:0

Before passAnRef obj is:0
After passAnRef obj is:10

Before passAnPtr obj is:10
After passAnPtr obj is:20

like image 34
Alok Save Avatar answered Sep 20 '22 15:09

Alok Save