I have a class with an array of objects that I have made private (to follow good programming practice). In order to get properties from an array object, I have a getter that returns an object that i store in a temporary "copy" like so:
//Getter
Weapon const g_weapon(int i)
{
return weapons;
}
//Calling from another method
Weapon w_temp = deck.g_weapon(i);
w_temp.get_some_property(); //etc.For memory efficiency, I would like to reference the object instead:
//Getter
Weapon &g_weapon(int i)
{
Weapon &weapon_ref = weapons;
return weapon_ref;
}
//Calling from another method
Weapon &w_ref = deck.g_weapon(i);
w_ref.get_some_property(); //etc.My questions are:
1 - Is it more memory efficient to use references in the way that I am?
2 - Would I be better off to make the array of Weapon objects public to be begin with? (All of the weapon properties are private with getters and setters)
3 - Also, what is considered best practice?
Thanks,
Josh



