First, as a general rule, prefer references over pointers. Even if you have to use pointers, as in references aren't an option, then something like
std::unique_ptr or
std::shared_ptr would be better to use.
Now, the most common use case for pointers / references are to prevent copies. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// an example world class
class World {
public:
// Functions, constructors, etc, etc, etc
private:
// a list of events in the current world - could be very large
std::vector<Event> _evts;
};
// an unrelated function that needs access to the world, but doesn't modify
void doSomethingWithValue(World w) {
// do something...
}
// the same function as above
void doSomethingWithReference(const World& w) {
// do something...
}
|
In the above case, the second function will in general have a much better performance than the first one. This is because the first one requires you to copy the world, including that potentially massive list of objects. However, for the second one, all you need to copy is a small bit of data containing the address in memory of the object.
In general, for functions, anything other than the basic data types (e.g. int, char*, float, etc) should be passed by reference. However, apart from basic 'mindless' optimizations such as that which increase speed at no loss of productivity (a mere 6 letters more, including the optional
const), try not to excessively optimize your code unless you have found a significant bottleneck.
On a fairly unrelated note, what version of OpenGL are you using? If you are using any version older than version 3.0, I would suggest starting to learn OpenGL again with a more recent tutorial (such as the one at arcsynthesis: www.arcsynthesis.org/gltut/ ). If you don't know, if you see anything in your program like
glBegin,
glVertex3f, and so on, you are probably using an old version of OpenGL. If you are so obsessed with performance, you'd definitely want to avoid the fixed pipeline of the older versions!