I have a custom Vector class that contains an x, y, and z. These are stored in an int array called data. So basically data[0] = x, data[1] = y, and data[2] = z. Simple enough.
I am tasked with writing a function that allows the following:
vector[0] = 2.0f;
Which should make the x value of the vector be 2.0. I’ve already overloaded the [ ] operator so that it returns a float (it’s essentially a getter method – ex:
cout << vector[0];
Would print whatever the x value of vector is.)
I’m confused as to how I can assign the float to the x, y or z. I think I need to overload an operator (perhaps the =), but I am not sure. Am I thinking about this the right way?
To do this, you need to provide a version of
operator[]that returns a reference to the member that you want to be assignable to. For example: