Alright so I've been getting deeply into C++ as of late and I'm getting everything down. Pointers are finally starting to make sense as far as when I should use them, how to implement them correctly, etc.
However, there was one little question about the fundamental use of pointers that I still had that needed answered. I'll jump right to the code:
With the following class A
and function foo(A* bar)
...
class A
{}
void foo(A* bar)
{}
... what's the difference between the following calls to foo
?
A* a;
A b;
foo(a);
foo(&b);
They both compile fine, and as far as I can remember I haven't had any issues with them.
I think that A b;
is instantiated right there, whereas A* a;
needs to be created with new
(since it hasn't actually created the object, it's just held a 4-byte long reference to a potential A
object).
I could, if I am thinking about this correctly, do a = b;
(EDIT make that a = &b
) and then successfully pass a
to foo
. But, if I don't do a = &b
and foo
tries to read the (non-existent) object pointed to by a
, it will causes runtime errors.
Also, if the above is correct, then it's assumed I can successfully call foo(&b);
just fine.
Am I correct?
Thanks!