I have a class to represent a one dimensional spectrum. The underlying datatype is a simple array of floats. To manipulate elements of the spectrum I overloaded the subscript operators as follows:
class OneDSpectrum
{
public:
...
float& operator[](int index);
const float& operator[](int index) const;
...
private:
int numChannels;
float* histogram;
...
}
Unfortunately, almost all uses of this class are as pointers so I have the somewhat annoying (*pointerToSpectrum)[ index ]
notation to deal with now.
I had two initial ideas:
My first idea was to overload the subscript operator for OneDSpectrum*
, but that just looks like a basket full of potential issues and trouble.
My second idea was to make some sort of custom smart pointer for OneDSpectrums.
So my question is this: Is it reasonable to do SmartPointerToSpectrum[ index ]
to get (*pointerToSpectrum)[ index ]
functionality? Or should I just deal with using a lot of (*pointerToSpectrum)[ index ]
?
Unfortunately, almost all uses of this class are as pointers
Have you considered using references instead?
void foo(OneDSpectrum* a, OneDSpectrum& b)
{
(*a)[0] = 42;
b[0] = 97;
}
int main()
{
OneDSpectrum x;
OneDSpectrum y;
foo(&x, y);
}