Consider the following code:
#include <iostream>
using namespace std;
void test0(const int &a){
cout << "const int &a passed *ptra "<< a << endl;
}
void test1(const int &a){
cout << "const int &a passed ptra "<< a << endl;
}
void test2(const int &a){
cout << "const int &a passed *a "<< a << endl;
}
void test3(const int &a){
cout << "const int &a passed a "<< a << endl;
}
void test4(int *a){
cout << "int *a passed &a "<< *a << endl;
}
void test5(int *a){
cout << "int *a passed a "<< a << endl;
}
void test6(int *a){
cout << "int *a passed *a "<< a << endl;
}
int main(){
int a = 3;
int *ptra;
*ptra = 3;
test0(*ptra);
//test1(ptra);
//test2(*a);
test3(a);
test4(&a);
//test5(a);
//test6(*a);
//int &b = a;
}
How come (int &a) when passed a becomes a reference? Is it essentially creating an int a and setting its address to the passed a? Wouldn't it make more sense for the resulting reference to point the address of the value of a (if a = 3 points to address 3), where if you passed like so:
void stuff(int &a);
int *ptr;
stuff(ptr);
a would set its address to the value passed (the address pointed to by ptr); What's the difference between a pointer and a reference?
What's more, if you uncomment //int &b = a the program ceases to output anything. Why?