运算符[][] C++
我想重载 operator[][]
以提供对 C++ 中 char 的 2D 数组的内部访问。
现在我只是重载 operator[]
,它的效果类似于
class Object
{
char ** charMap ;
char* operator[]( int row )
{
return charMap[row] ;
}
} ;
It Works ok.. 是否可以覆盖 operator[][]
?
I'd like to overload operator[][]
to give internal access to a 2D array of char in C++.
Right now I'm only overloading operator[]
, which goes something like
class Object
{
char ** charMap ;
char* operator[]( int row )
{
return charMap[row] ;
}
} ;
It works ok.. Is it possible to override operator[][]
though?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要尝试这样做 - 正如其他人所说,重载
operator []
实际上是免费提供[][]
语法。但这不是什么好事。相反,它通过将实现细节(
char*
指针)转向外部,破坏了类的封装和信息隐藏。一般来说,这是不可取的。一种更好的方法是实现一个带有多个参数的
操作符[,]
,或者实际上是一个操作符[][]
。但 C++ 中两者都不存在。因此,执行此操作的通常方法是在多个维度上完全放弃
操作符[]
。一种干净的替代方法是使用operator ()
来代替,因为该运算符可以有多个参数:有关详细信息,请参阅 C++ FAQ Lite。
Don’t try to do that – as others have said, overloading
operator []
the way you do actually provides the[][]
syntax for free. But that’s not a good thing.On the contrary – it destroys the encapsulation and information hiding of your class by turning an implementation detail – the
char*
pointer – to the outside. In general, this is not advisable.A better method would be to implement an
operator [,]
which takes more than one argument, or indeed anoperator [][]
. But neither exists in C++.So the usual way of doing this is to ditch
operator []
altogether for more than one dimension. The clean alternative is to useoperator ()
instead because that operator can have more than one argument:For more information, see the article in the C++ FAQ Lite.
没有运算符
[][]
:这是连续两个[]
操作。您可以:Object::operator[]
返回表示行的第二类的对象,该对象有自己的operator[]
方法,该方法采用列号;There is no operator
[][]
: that's two[]
operations in a row. You could:Object::operator[]
return an object of a second class representing a row, which has its ownoperator[]
method that takes a column number;get(int row, int column)
method and use that instead of operator overloading. I'd recommend this unless your object absolutely has to behave like an array.没有
操作符[][]
。评估a[x][y]
首先对a
调用operator[]
,然后再次调用operator[]
就其结果而言。因此,对象的
operator[]
必须返回另一个具有自己的operator[]
的对象,然后该对象将访问请求的值。There is no
operator[][]
. Evaluatinga[x][y]
first callsoperator[]
ona
, and thenoperator[]
again on the result of that.So the
operator[]
of your object has to return another object with its ownoperator[]
, which then will access the requested value.据我所知,不存在
operator[][]
这样的东西。您可以做的是从operator[]
方法返回已重载operator[]
的内容。实际上你现在正在这样做,因为你返回了
char*
,它可以再次使用[]
进行索引。As far as I know there is no such thing as
operator[][]
. What you can do is you could return from youroperator[]
method something that has overloadedoperator[]
.Actually you are doing it now, because you return
char*
which can be indexed using[]
again.没有
[][]
运算符。实际发生的情况是,第二个[]
对第一个[]
返回的变量进行操作。因为已经存在该功能,所以如果存在[][]
运算符,就会产生歧义。例如:假设您有一个类型为
T
的变量x
。如果我们使用
[]
运算符,假设返回其他类型Q
的变量:然后对变量使用
[]
运算符Q
类型可能返回一个R
类型的变量:因此
x[][]
返回一个 R 类型的变量。假设我们实际上能够重载类型 T 的
[][]
,使其返回类型 S:编译器无法知道是否应该使用
[][]
x
上的运算符返回类型S
变量,或连续使用[]
运算符两次返回类型R< /代码> 变量。这就是我上面提到的模糊性。
如果您坚持使用方括号,最好的选择是让
operator[]
返回一个也重载了[]
的变量(或者也许是相同类型的变量,设置了标志),并让最初返回的变量处理第二个[]
。但这里最好的解决方案(正如另一个答案中已经提到的)是使用不同的运算符,例如如
()
。There is no
[][]
operator. What actually happens is that the second[]
operates on the variable returned by the first[]
. Because there is already that functionality, it would create ambiguity were there to exist a[][]
operator.For example: let's say you have a variable
x
of some typeT
.If we use the
[]
operator, let's say a variable of other typeQ
is returned:And then using the
[]
operator on a variable of typeQ
might return a variable of typeR
:Therefore
x[][]
returns a variable of t ype R.Let's say we actually were able to overload
[][]
for type T such that it returned a type S:The compiler would have no way of knowing if it should use the
[][]
operator onx
to return a typeS
variable, or use the[]
operator twice in a row to return a typeR
variable. This is the ambiguity I mentioned above.Your best bet if you're stuck on using square brackets is to have
operator[]
return a variable which also has[]
overloaded (or perhaps a variable of the same type, with a flag set), and have that initially returned variable deal with the second[]
.But the best solution here (as mentioned already in another answer) is to use a different operator such as
()
.