运算符[][] C++

发布于 2024-08-27 09:02:45 字数 314 浏览 4 评论 0原文

我想重载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青春如此纠结 2024-09-03 09:02:45

不要尝试这样做 - 正如其他人所说,重载 operator [] 实际上是免费提供 [][] 语法。但这不是什么好事。

相反,它通过将实现细节(char* 指针)转向外部,破坏了类的封装和信息隐藏。一般来说,这是不可取的。

一种更好的方法是实现一个带有多个参数的操作符[,],或者实际上是一个操作符[][]。但 C++ 中两者都不存在。

因此,执行此操作的通常方法是在多个维度上完全放弃操作符[]。一种干净的替代方法是使用 operator () 来代替,因为该运算符可以有多个参数:

class Object
{
    char ** charMap ;
    char& operator ()(int row, int column)
    {
        return charMap[row][column];
    }
};

有关详细信息,请参阅 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 an operator [][]. 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 use operator () instead because that operator can have more than one argument:

class Object
{
    char ** charMap ;
    char& operator ()(int row, int column)
    {
        return charMap[row][column];
    }
};

For more information, see the article in the C++ FAQ Lite.

浪推晚风 2024-09-03 09:02:45

没有运算符 [][]:这是连续两个 [] 操作。您可以:

  • Object::operator[] 返回表示行的第二类的对象,该对象有自己的 operator[] 方法,该方法采用列号;
  • 编写一个 get(int row, int column) 方法并使用它来代替运算符重载。我建议这样做,除非你的对象绝对必须表现得像一个数组。

There is no operator [][]: that's two [] operations in a row. You could:

  • Have Object::operator[] return an object of a second class representing a row, which has its own operator[] method that takes a column number;
  • Write a 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.
嘿哥们儿 2024-09-03 09:02:45

没有操作符[][]。评估 a[x][y] 首先对 a 调用 operator[],然后再次调用 operator[]就其结果而言。

因此,对象的 operator[] 必须返回另一个具有自己的 operator[] 的对象,然后该对象将访问请求的值。

There is no operator[][]. Evaluating a[x][y] first calls operator[] on a, and then operator[] again on the result of that.

So the operator[] of your object has to return another object with its own operator[], which then will access the requested value.

何以心动 2024-09-03 09:02:45

据我所知,不存在 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 your operator[] method something that has overloaded operator[].

Actually you are doing it now, because you return char* which can be indexed using [] again.

安穩 2024-09-03 09:02:45

没有 [][] 运算符。实际发生的情况是,第二个 [] 对第一个 [] 返回的变量进行操作。因为已经存在该功能,所以如果存在 [][] 运算符,就会产生歧义。

例如:假设您有一个类型为 T 的变量 x

T x = new T();

如果我们使用 [] 运算符,假设返回其他类型 Q 的变量:

Q y = x[0];

然后对变量使用 [] 运算符Q 类型可能返回一个 R 类型的变量:

R z = y[0];

因此 x[][] 返回一个 R 类型的变量。

假设我们实际上能够重载类型 T 的 [][],使其返回类型 S:

S a = x[0][0];

编译器无法知道是否应该使用 [][] 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 type T.

T x = new T();

If we use the [] operator, let's say a variable of other type Q is returned:

Q y = x[0];

And then using the [] operator on a variable of type Q might return a variable of type R:

R z = y[0];

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:

S a = x[0][0];

The compiler would have no way of knowing if it should use the [][] operator on x to return a type S variable, or use the [] operator twice in a row to return a type R 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 ().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文