重载[]运算符 [英] overloading the [] operator

查看:94
本文介绍了重载[]运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我已经创建了一个用于存储二维矩阵的类,而且我一直在尝试重载[]
运算符,因此访问

矩阵的元素很容易。我有3个私有变量,_m,_row和_col分别是存储数据的数组,行数和

列数。 _m的类型为T,大小为_row * _col * sizeof

(T)。


我设法重载了这样的下标运算符:

T& operator [](int n)

{

return(_m [n * _col]);

}

但我不明白你为什么不能这样做:

T * operator [](int n)

{

返回(& _m [n * _col]);

}


代替。第一个工作正常,但是第二个工作时我尝试了一下像m [0] [0] = 100.0那样的
,m [0] [0]出现了无效

左右。

有人可以帮帮我吗?


干杯,


- 约瑟夫帕特森

解决方案

Joseph Paterson写道:

你好,
矩阵的元素。我有3个私有变量,_m,_row和_col分别是存储数据的数组,行数和列数。 _m的类型为T,大小为_row * _col * sizeof
(T)。

我设法使下标运算符重载如下:
T& operator [](int n)
{
返回(_m [n * _col]);
}

但我不明白为什么你能'不要这样做:
T * operator [](int n)
{
返回(& _m [n * _col]);
}
相反。第一个工作正常,但第二个工作时,当我尝试像m [0] [0] = 100.0这样的东西时,m [0] [0]出现一个无效的
左值。 / blockquote>


请点击FAQ。在这个

组中,这个话题一再被打死,每隔一段时间就会引起一点点火焰。

的主旨是:


a)考虑使用operator()(row,col)而不是operartor [] []。它更容易实现干净:[] [] - 语法的问题是,首先尝试使用

,你将选择一个快速而肮脏的解决方案[]

返回类似向量的内容(正如您在帖子中指出的那样)。这是

绑定来公开矩阵类的实现细节,这被认为是设计不佳的
。详细信息在常见问题解答中。


b)如果你是头脑冷静/意志坚定并决心支持

[] [] - 语法,超载operator []返回一个代理对象,该对象包含对矩阵的

引用并知道该行。代理类本身将使用b $ b重载operator [](const)来返回对该条目的(const)引用。

这个技巧允许分离实现和接口。详细信息在

常见问题解答中。


c)如果您的矩阵类不仅仅是一个容器,即

您计划支持线性代数,请考虑使用

许多可用库之一。一个完整的矩阵类是一个相当大的难度的项目。除非你正在寻找一个角色建设和高度的教育锻炼,否则不要在没有合法需要的情况下给自己打电话。

Best


Kai-Uwe Bux


我实际上正在尝试实现一个完全成熟的矩阵类。我是从图形引擎开始的
,我想完全支持矩阵和

你可以做的所有可能的操作:)这很有趣,和学习

体验。


我会看看常见问题 - 谢谢!


- Joseph Paterson


Joseph Paterson写道:

我实际上正在尝试实现一个完全成熟的矩阵类。


好​​吧。

我开始使用图形引擎,我想完全支持矩阵
以及所有可能的操作对他们做:)这很有趣,也是一种学习经验。


好​​的,你要求它:


a)你可能想要阅读表达模板。当涉及到诸如添加矢量/矩阵之类的东西时,真正引入临时值的b / b
会成为性能问题。表达模板在大多数状态下使用艺术矩阵库来处理这个问题。如果你真的是b $ b前往图形引擎,性能将很快成为一个问题。


b)至于语法糖(如[] [] - 注意),我发现

对列和行向量进行分类的最有趣的用途是

支持基本操作如下:


A.row(i)+ = some_scalar * A.row(j);





swap (A.col(i),A.col(j));


然而,要使这项工作有点挑战。


我会看看常见问题 - 谢谢!




这是一个很好的开始。您可能还需要注意浮点运算对b
的看法(假设你很可能是b $ b b b必然要做数值线性代数)。请注意,数值线性

代数是困难的,并且有许多陷阱(超出FAQ中的陷阱)

很难发现。什么看起来像你的线性

代数文本的正确算法可能会在数值上不稳定。

玩得开心


启-Uwe Bux


Hello,

I''ve created a class to store 2 dimensional matrices, and I''ve been
trying to overload the [] operator, so access to the elements of the
matrix is easy. I have 3 private variables, _m, _row and _col which are
respectively the array to store the data in, the number of rows and the
number of columns. _m is of type T, and is size _row * _col * sizeof
(T).

I''ve managed to overload the subscript operator like this:
T & operator [] (int n)
{
return (_m[n * _col]);
}

but I don''t understand why you can''t do:
T * operator [] (int n)
{
return (&_m[n * _col]);
}

instead. The first one works fine, but with the second one when I try
something like m[0][0] = 100.0, m[0][0] comes up as being an invalid
lvalue.
Could someone help me out here?

Cheers,

- Joseph Paterson

解决方案

Joseph Paterson wrote:

Hello,

I''ve created a class to store 2 dimensional matrices, and I''ve been
trying to overload the [] operator, so access to the elements of the
matrix is easy. I have 3 private variables, _m, _row and _col which are
respectively the array to store the data in, the number of rows and the
number of columns. _m is of type T, and is size _row * _col * sizeof
(T).

I''ve managed to overload the subscript operator like this:
T & operator [] (int n)
{
return (_m[n * _col]);
}

but I don''t understand why you can''t do:
T * operator [] (int n)
{
return (&_m[n * _col]);
}

instead. The first one works fine, but with the second one when I try
something like m[0][0] = 100.0, m[0][0] comes up as being an invalid
lvalue.



Please, hit the FAQ. This topic has been beaten to death repeatedly in this
group and gives rise to a little bit flaming every once in a while. The
gist of it is:

a) Consider using operator()( row, col ) instead of operartor[][]. It is
easier to implement cleanly: the problem with [][]-syntax is that upon a
first try, you will settle on a quick and dirty solution where operator[]
return something like a vector (just as you indicate in your post). This is
bound to expose implementation details of the matrix class, which is
considered poor design. Details are in the FAQ.

b) If you are hard-headed / strongly-willed and determined to support the
[][]-syntax, overload operator[] to return a proxy object that contains a
reference to your matrix and knows the row. The proxy class itself will
overload operator[] (const) to return a (const) reference to the entry.
This trick allows to separate implementation and interface. Details are in
the FAQ.

c) If your matrix class aims to be more than merely a container, i.e., if
you plan on supporting linear algebra, please consider using one of the
many available libraries. A full-fledged matrix class is a project of
considerable size and difficulty. Don''t call that upon yourself without
legitimate need unless you are looking for a character building and highly
educational exercise.
Best

Kai-Uwe Bux


I actually am trying to implement a fully fledged matrix class. I''m
starting on a graphics engine, and I want to fully support matrices and
all possible operations you can do on them :) It''s fun, and a learning
experience.

I''ll have a look at the FAQ - thanks!

- Joseph Paterson


Joseph Paterson wrote:

I actually am trying to implement a fully fledged matrix class.
Oh well.
I''m starting on a graphics engine, and I want to fully support matrices
and all possible operations you can do on them :) It''s fun, and a learning
experience.
Ok, you asked for it:

a) You may want to read up on expression templates. When it comes to things
like adding vectors / matrices, the introduction of temporaries truly
becomes a performance issue. Expression templates are used in most state of
the art matrix libraries to deal with this problem. If you are really
headed for a graphics engine, performance will become an issue pretty soon.

b) As far as syntactic sugar (like [][]-notation) is concerned, I found that
the most interesting use of proxy classed for column and row vectors is to
support elementary operations like so:

A.row(i) += some_scalar * A.row(j);

or

swap( A.col(i), A.col(j) );

However, to make that work is a little bit challenging.

I''ll have a look at the FAQ - thanks!



That is a good point to start. You may also want to pay attention to what it
has to say about floating point arithmetic (given that you are very likely
bound to do numerical linear algebra). Be warned that numerical linear
algebra is difficult and there are many traps (beyond the ones in the FAQ)
that are hard to spot. What looks like a correct algorithm from your Linear
Algebra text can turn out to be numerically unstable.
Have fun

Kai-Uwe Bux


这篇关于重载[]运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆