mardi 21 avril 2015

Reason to have a member reference variable over a member pointer variable

What is a practical example and reason to store a reference to an object instead of a pointer? The obvious reason I can come up with is to avoid memory management.

I've come across a library where sometimes a class will store an object by reference and sometimes by pointer and it's difficult for me to understand why and when to use one over the other?

Example:

class EventBusDemo : public Object
{
private:
    HandlerRegistration* playerMoveReg; 
};

class PlayerMoveEvent : public Event
{
private:
    Player& player;  // Why not a pointer here?
};

Aucun commentaire:

Enregistrer un commentaire