jclancy
jclancy

Reputation: 52318

Defining the []= operator

I'm writing a class Grid whose elements are Points - an (int) grid each of whose squares has a (double) point in it. I've already defined this (the height value is stored elsewhere):

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

and I want to define the assignment operator. How would one go about this? Is it defined automatically based on the [] operator?

So far I have

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

but that seems like a circular definition.

Upvotes: 5

Views: 1668

Answers (1)

Nim
Nim

Reputation: 33655

That's not how it works, the [] operator should return a reference the element at that index, and that element (type) should support operator= (i.e. Point::operator=)

Upvotes: 8

Related Questions