There is no performance difference between pointers and references. References are just pointers in disguise. The only difference is that pointers allow
nullptr as return types, whereas there is no such thing as a "nullref".
My rule of thumb is: If a nullptr is absolutely required, or if you need to do pointer arithmetic, use pointers. In any other case use references.
References are
probably more idiomatic in C++. They are more tightly bound, there are no extra steps involved in getting the value, and they force the programmer to always have initialised values (unlike with pointers). This is compiler enforced.
With function arguments, passing by const reference should always be used over passing by value, unless the passed value is a
POD.
Const reference:
void foo(const Data& myData)
{
}
// this allows the following to work, where Data is a class/struct with a constructor
foo(Data(1, 6));
Lastly, there's my aesthetic view on references. I think my code looks better like this:
world.getEntityManager().createEntity()
.addComponent(new Position(0.0, 0.0))
.addComponent(new Velocity(0.1, 0.2))
.addComponent(new Name("GAyLord"))
.addComponent(new Sprite("res/gaylord.png"))
Than this:
world->getEntityManager()->createEntity()
->addComponent(new Position(0.0, 0.0))
->addComponent(new Velocity(0.1, 0.2))
->addComponent(new Name("GAyLord"))
->addComponent(new Sprite("res/gaylord.png"))
I like offending people. People who get offended should be offended. --
Linux Torvalds