Are the C++ & and * operators inverses in all contexts?

int *newVariable;

and

int* newVariable;

Are just the same thing. so does:

int& newReference;
int &newReference;
int & newReference;

And once you declared the reference, you can think of it just like the actual variable itself. so you don’t need particular operator to dereference it.

when passing variables through function arguments, the raw object is always shallow copied in memory to the new variable in the function scope if it’s not a reference (in the case of a pointer, this just means an address is copied).

When passing a variable through function argument, it all depends how the function acquires that variable. i.e. the function definition:

so there’s a difference between this:

void func(int parameter);

and this:

void func(int & parameter);

The first one just copies the parameter to function scope, so any change in the parameter, won’t affect the actual variable you passed in. but in the case of second one, it’s the reference which is acquired by func() so it’s changes will affect your variable too.

Leave a Comment