过载[] [英] overload []

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

问题描述

..




有可能超载[]运算符,但我想知道是否可能

以某种方式重载[] []等等。


我的目标是从静态数组声明转换成某种东西

totaly动态,代码变化极小。


以下代码


int foo [NB_ROW] [NB_COL];


foo [12] [3] = 0;


将成为:


Array2DimInt foo(NB_ROW,NB_COL) ;


foo [12] [3] = 0;


我有意义吗?


- 马里奥

..
Hi,

It''s possible to overload the [] operator, but I wonder if it''s possible to
somehow overload [][] and so forth.

My goal would be to switch from static array declaration into something
totaly dynamic with minimal change in the code.

The following code

int foo[NB_ROW][NB_COL];

foo[12][3] = 0;

would become:

Array2DimInt foo(NB_ROW, NB_COL);

foo[12][3] = 0;

Am I making sense?

- Mario


推荐答案



" Mario Charest" < PO ******** @ 127.0.0.1>在消息中写道

新闻:Yy ********************* @ wagner.videotron.net。 ..

"Mario Charest" <po********@127.0.0.1> wrote in message
news:Yy*********************@wagner.videotron.net. ..



有可能重载[]运算符,但我想知道它是否可能是
以某种方式超载[] []等等。

我的目标是从静态数组声明切换到完全动态且代码变化最小的东西。

int foo [NB_ROW] [NB_COL];

foo [12] [3] = 0;

将成为:

Array2DimInt foo(NB_ROW,NB_COL);

foo [12] [3] = 0;

我有意义吗?

- 马里奥
.
Hi,

It''s possible to overload the [] operator, but I wonder if it''s possible to somehow overload [][] and so forth.

My goal would be to switch from static array declaration into something
totaly dynamic with minimal change in the code.

The following code

int foo[NB_ROW][NB_COL];

foo[12][3] = 0;

would become:

Array2DimInt foo(NB_ROW, NB_COL);

foo[12][3] = 0;

Am I making sense?

- Mario




尝试


使用命名空间std;


vector<矢量< INT> > foo;


它完全是动态的。在你有数据之后,你可以像下面这样使用它:


foo [12] [3] = 42;


然而,索引不应该超出范围



try

using namespace std;

vector < vector < int> > foo;

it is totally dynamic. after you have data in, you can use it like:

foo[12][3] = 42;

however, the indexes shouldn''t be out of range




" Mario Charest" < PO ******** @ 127.0.0.1>在消息中写道

新闻:Yy ********************* @ wagner.videotron.net。 ..

"Mario Charest" <po********@127.0.0.1> wrote in message
news:Yy*********************@wagner.videotron.net. ..



有可能重载[]运算符,但我想知道它是否可能是
以某种方式超载[] []等等。

我的目标是从静态数组声明切换到完全动态且代码变化最小的东西。

int foo [NB_ROW] [NB_COL];

foo [12] [3] = 0;

将成为:

Array2DimInt foo(NB_ROW,NB_COL);

foo [12] [3] = 0;

我有意义吗?

- 马里奥
.
Hi,

It''s possible to overload the [] operator, but I wonder if it''s possible to somehow overload [][] and so forth.

My goal would be to switch from static array declaration into something
totaly dynamic with minimal change in the code.

The following code

int foo[NB_ROW][NB_COL];

foo[12][3] = 0;

would become:

Array2DimInt foo(NB_ROW, NB_COL);

foo[12][3] = 0;

Am I making sense?

- Mario



嗨马里奥,


直接超载[]是不可能的[]。但是,你可以重载[]到

返回一个[]也有意义的类型,从而合成[] [] for

封闭类。这就是Dan's sol''n为你做的,使用

标准库,这样你就不必做这项工作了。


肖恩。


Hi Mario,

It''s not possible to directly overload [][]. However, you can overload [] to
return a type for which [] is also meaningful, thus synthesizing [][] for
the enclosing class. That''s what Dan''s sol''n does for you, using the
standard lib so you don''t have to do the work.

Sean.


Mario Charest写道:
Mario Charest wrote:



有可能使[]运算符超载,但我想知道是否有可能以某种方式超载[] []等等。

我的目标是从静态数组声明切换到一些东西
完全动态,代码变化很小。

以下代码

int foo [NB_ROW] [NB_COL];

foo [12] [3] = 0;

将成为:

Array2DimInt foo(NB_ROW,NB_COL);
foo [12] [3] = 0;

.
Hi,

It''s possible to overload the [] operator, but I wonder if it''s possible to
somehow overload [][] and so forth.

My goal would be to switch from static array declaration into something
totaly dynamic with minimal change in the code.

The following code

int foo[NB_ROW][NB_COL];

foo[12][3] = 0;

would become:

Array2DimInt foo(NB_ROW, NB_COL);

foo[12][3] = 0;




向量< vector< int> > Dan建议可以使用,但对于矩阵来说它有点太多了(每行存储它自己的维度,你需要关注
)。在某些情况下,其次优的内存使用量也可能是一个因素
。这里还有两个选择:


a)(肖恩打败了我,但是我打算发布这个,因为我已经输入了
它已经)。

使第一个[]返回一个代理对象。这是一个示例的想法

(感觉没有把它变成模板):


模板< typename T>

class Array2Dim {

vector< T> data_;

int width_;

public:

class RowProxy {

friend class Array2Dim;

vector< T>& data_;

int ofs_;

RowProxy(vector< T>& data,int ofs):data_(data),ofs_(ofs){}

public:

T& operator [](int col){

返回data_ [ofs_ + col];

}

};


RowProxy运算符[](int行){

返回RowProxy(data_,row * width_);

}

/ / ...

};


b)考虑使用(i,j)表示法而不是[i] [j]。我更喜欢

[i] [j]语法,但是(i,j)在实现方面更胜一筹。

(使用operator()(int row,int col) ))。

虽然你提到你想尽量减少对现有

代码的更改,但这可能不适合你。


Denis



A vector<vector<int> > that Dan suggested would work, but it is a bit too
generic for matrices (each row stores its own dimension, which you need
to care about). In some cases its suboptimal memory usage can be a factor
as well. Here are two more choices:

a) (Sean beat me to it, but I''m going to post this anyway since I''ve
typed it already).
Make the first [] return a proxy object. Here is an example idea
(it didn''t feel right not making it a template):

template <typename T>
class Array2Dim {
vector<T> data_;
int width_;
public:
class RowProxy {
friend class Array2Dim;
vector<T>& data_;
int ofs_;
RowProxy(vector<T>& data, int ofs) : data_(data), ofs_(ofs) {}
public:
T& operator [](int col) {
return data_[ofs_+col];
}
};

RowProxy operator [](int row) {
return RowProxy(data_, row*width_);
}
//...
};

b) Consider using the (i, j) notation instead of [i][j]. I prefer the
[i][j] syntax, but (i, j) is superior in its implementation.
(Use operator() (int row, int col)).
Though as you mentioned you wanted to minimise the change to the existing
code, this may not work well for you.

Denis


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

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