Operator Definition [] =

I am writing a Grid class whose elements are Points - a (int) grids, each of whose squares has a (double) point in it. I already defined this (the height value is saved elsewhere):

Point &operator[](Point p) { return floor(get_x(p)) + height * floor(get_y(p)); } 

and I want to define an assignment operator. How can I do that? Is this automatically determined based on the [] operator?

I still have

 Point &operator[]=(Point p, Point q) { data[floor(get_y(p)) * height + floor(get_x(p))] = q; } 

but it looks like a circular definition.

+4
source share
1 answer

Not the way this works, the [] operator should return a reference to the element in this index, and this element (type) should support operator= (i.e. Point::operator= )

+7
source

All Articles