I am getting the following error:
Conversion loses qualifiers
when trying to implement indexing operator without the code duplication (I will show the segment of code):
Point* BufferedList::indexTemp(size_t idx)
{
if (idx >= size) return nullptr;
return &arr[idx];
}
const Point* BufferedList::operator [](size_t idx) const
{
return indexTemp(idx);
}
Point* BufferedList::operator [](size_t idx)
{
return indexTemp(idx);
}
However, the following works (this doesn't use the helper function indexTemp
and it is duplication of code):
const Point* BufferedList::operator [](size_t idx) const
{
if (idx >= size) return nullptr;
return &arr[idx];
}
Point* BufferedList::operator [](size_t idx)
{
if (idx >= size) return nullptr;
return &arr[idx];
}
Do I really need two functions for indexing (one that returns Point*
and the other that returns const Point*
)?