+ 1
Why we use pointers?
Why don't we use the simple n.attack(); formula?
3 Réponses
+ 1
we use pointers for a few reasons. one is to access variables not in our current scooe, another is because we might want it to live on the memory past it's function call. as far as the specific example in your question I'm not sure either should be fine. it was more or less a way to show you pointer classes
0
Pointers allow us to pass the address of an object around.
Think of this from a low level point of view. A pointer is a 32 bit number on a 32 bit machine. To pass around a reference to that object means we only need to copy around that 32 bit number. The actual object that address points to could be any size.
Passing around the actual object could mean copying literally millions of bytes. Using the pointer is a far more efficient way of using CPU instructions.