|
Re: C++ help: Understanding pointers?
Pointers are exactly what they sound like, if you have encountered 'References' a pointer is very similar. A pointer is literally the memory address of a piece of data, the type you assign to the pointer dictates what you expect to find at that address (such as a class, an array, or an integer).
You use a pointer typically to avoid moving around big chunks of memory. If you imagine the basic robot class, it has a large number of variables for motors drive systems etc, all of these are stored in memory. If you call a funtion and pass in the ACTUAL robot, you end up making a copy of the whole robot (remember if you change a variable you pass into a function, you are changing a copy, not the original) which can be a lot of memory to copy and is quite wasteful. If you simply pass a pointer, you essentially say "our robot is located a memory address 0x800f000" and that pointer (the address only) becomes the variable which is copied into memory, when you look up that memory address you get the real robot, so if you change attributes of the robot, you are not changing a copy, but rather the real robot.
A semi-similar example would be if you were trying to send someone a file you downloaded from the internet, you could either email the file itself (giving them a copy of the same file) or email them a link to the file. In both cases they have access to the file, but the latter is more efficient.
|