I only recently started learning C++ however from what I know up till now I tend to look at C++ "passed-by-reference" as basically const pointers. They both accomplish the same thing but one requires less code. Doesn't mean pointers cannot accomplish the same thing
Code:
1 #include<iostream>
2 using namespace std;
3
4 void foo(int& a,int& b){
5
6 a=0;
7 b=0;
8 }
9
10 void foo_ptrs(int* a, int * b){
11
12 *a = 0;
13 *b = 0;
14 }
15
16 int main(void){
17
18 int a = 1, b = 1;
19
20 foo(a,b);
21 cout << "using foo() a = " << a <<" b ="<< b <<endl;
22
23 foo_ptrs(&a,&b);
24 cout <<"using foo_ptrs() a = " << a << " b = " << b << endl;
25
26 return 0;
27 }
A reference does the same thing as a pointer in that it allows you to access the address of a variable and change it from within function scope. However
It is supposed to be "safter" in that you cannot change the value it is pointing to. Whereas in C you can. C++ folks will argue its merits but for now
I simply use it and see it as or .
Always opened to hear more from those who've been working with it longer though. I'm still a C fan boy at heart