- #1
Edgardo
- 706
- 17
I am confused by two versions of call by reference in C++. As an example two methods that square an integer (CBR stands for Call By Reference):
Let's call the functions:
1) What I don't understand is that I'm calling squareCBR(int& a)
by writing squareCBR(a). Why just a? I thought I'd have to pass an address to the function since the argument is (int& a).
2) The same question about squareCBR2(int* x). I call this function by writing squareCBR2(&x). Why &x? I thought I'd have to pass a pointer since the argument is (int* x).
Code:
//Version 1
void squareCBR(int& x)
{
x = x * x;
}
//Version 2
void squareCBR2(int* x)
{
(*x) = (*x) * (*x);
}
Let's call the functions:
Code:
int x = 3;
squareCBR(x);
squareCBR2(&x);
1) What I don't understand is that I'm calling squareCBR(int& a)
by writing squareCBR(a). Why just a? I thought I'd have to pass an address to the function since the argument is (int& a).
2) The same question about squareCBR2(int* x). I call this function by writing squareCBR2(&x). Why &x? I thought I'd have to pass a pointer since the argument is (int* x).
Last edited: