Hello,
with the const correctness chapter of the C++ FAQ in mind, i think about redesigning my getters.
I have the following code example:
Code:
template<typename T>
class Container
{
public:
// prefer this one?
T getter1(const int index) const
{
return data[index];
}
// or this one by const ref?
T const &getter2(const int index) const
{
return data[index];
}
private:
T *data;
};
Is it genereally more appropriate to return by const reference or by value?
As much as i know, a reference can never be NULL, so if my data might be NULL in any point in the data array, i should rather return by value. Is this the only criteria to choose between the two?