简体   繁体   中英

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.

这不是它的工作方式, []运算符应该返回该索引处元素的引用,并且该元素 (类型)应该支持operator= (即Point::operator=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM