Babra Cunningham Babra Cunningham - 9 months ago 61
C++ Question

Pass by reference confusion?

Consider this example:

void MyFunction(int &x){...}

void MyOtherFunction(int *x){...}

int main()
int my_variable = 10;

Am I right in saying both the these methods are passing the variable by reference (i.e. not creating a copy), and the only difference is that MyOtherFunction is receiving a pointer?

Answer Source

You're right in saying that you don't copy my_variable, but a pointer is not a reference, they're two sets of distinct types.

For example, a pointer can be null and you can call MyOtherFunction(nullptr).