A function returns a reference on int
int& MyClass::getElement(int position)
{
if (position < _size)
return (_array[position]);
return ([...]) // An Error
}
My first think was to return NULL. But obviously a reference can not be NULL.
What is the right way to return an error in this case?
Various options, roughly ordered with my preferred options first:
std::out_of_range
std::pair<bool,int&>
or boost::optional<int&>
. This is more useful if you want to return something by value, not reference, so can't return a pointer.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With