简体   繁体   English

定义[] =运算符

[英]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. 我正在编写一个Grid类,其元素是Points - 一个(int)网格,每个网格的正方形都有一个(双)点。 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=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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