boom
boom

Reputation: 6166

Regarding address operator C/C++

What does address operator mean.

say in the method below.

what should be passed in the method as parameter value of integer or the address of an integer variable.

void func1(int&)// method declaration

void func1(int& inNumber)//method definition
{ 
//some code
}

Upvotes: 0

Views: 203

Answers (5)

anish
anish

Reputation: 1033

its called as reference in c++ http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

Upvotes: 0

Thomas
Thomas

Reputation: 182028

That's not an address operator. In a declaration, the & means it's a reference.

You can think of references as pointers that you don't have to dereference.* You can just say inNumber = 5; (instead of *inNumber = 5;) and the effect will be visible outside the function.

* Note: References are not just "pointers that you don't have to dereference". It just helps to think of them in that way, sometimes.

Upvotes: 0

RC.
RC.

Reputation: 28267

In this case the function takes a reference to an int which is denoted by the int &inNumber.

The function is called as if you were calling it with the value:

int x = 2;
func1(x);

From the perspective of the caller, this looks exactly the same as a pass by value function, though in the case of the reference the function may indeed change the value of x.

Upvotes: 0

anon
anon

Reputation:

There is no address-of operator in your code - the ampersand is being used to declare a reference. Which C++ text book are you using that does not cover this?

Upvotes: 3

Konrad Rudolph
Konrad Rudolph

Reputation: 545963

That’s not the address operator – it’s the reference type character. This means that for any type T, T& is a reference to T. It’s an unlucky coincidence that this happens to be the same character as the address operator.

You can pass normal objects to this method. No need to take any further action. Read up on references in your favourite C++ book.

Upvotes: 11

Related Questions