By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

overload []

P: n/a
..
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


Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Mario Charest" <po********@127.0.0.1> wrote in message
news:Yy*********************@wagner.videotron.net. ..
.
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


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
Jul 22 '05 #2

P: n/a

"Mario Charest" <po********@127.0.0.1> wrote in message
news:Yy*********************@wagner.videotron.net. ..
.
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

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.
Jul 22 '05 #3

P: n/a
Mario Charest wrote:

.
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;


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
Jul 22 '05 #4

P: n/a

"Mario Charest" <po********@127.0.0.1> wrote in message
news:Yy*********************@wagner.videotron.net. ..
.
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


The proxy class idea that others have suggested is a good general purpose
solution.

However in your particular case there is a simpler answer. Just have your
operator[] return a pointer the start of a row. So in an expression like

foo[12][3]

the first operator[] is your operator[] for the class, and the second
operator[] is just the regular operator[] that works on the pointer returned
by the first operator[].

john
Jul 22 '05 #5

P: n/a
John Harrison wrote:

"Mario Charest" <po********@127.0.0.1> wrote in message
news:Yy*********************@wagner.videotron.net. ..
.
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


The proxy class idea that others have suggested is a good general purpose
solution.

However in your particular case there is a simpler answer. Just have your
operator[] return a pointer the start of a row. So in an expression like

foo[12][3]

the first operator[] is your operator[] for the class, and the second
operator[] is just the regular operator[] that works on the pointer returned
by the first operator[].

john


Thanks! I completely missed it. The part I like the most is that you still can
use a vector as one contiguous storage.

Denis
Jul 22 '05 #6

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2g************@uni-berlin.de...

"Mario Charest" <po********@127.0.0.1> wrote in message
news:Yy*********************@wagner.videotron.net. ..
.
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


The proxy class idea that others have suggested is a good general purpose
solution.

However in your particular case there is a simpler answer. Just have your
operator[] return a pointer the start of a row. So in an expression like

foo[12][3]

the first operator[] is your operator[] for the class, and the second
operator[] is just the regular operator[] that works on the pointer

returned by the first operator[].
I like the simplicity of the solution but it would not work in my case.

The arrays are used to map an image coming from a camera. Depending on
various factor the camera could be rotate 90 180 or 270 degrees. The
software current has different code path to handle the different angle, it's
a pain to maintain. I would like foo[12][3] to return the same pixel value
what ever the image angle (rotating the image beforehand is out of the
question for performance reason).

In that case using vector is out of the question. I think what I'll end up
doing is an image class with a Pixel( int x, int y ) member and I'll have a
class per angle. This means lots of change in the software but it doesn't
look like I have a choice.

Seems to me like only being able to overload [] is an oversight. If you can
overload one [] you should be able to overload [][], oh well.

Thanks everyone.


john

Jul 22 '05 #7

P: n/a

"Woebegone" <wo*************@THIScogeco.ca> wrote in message
news:rP*******************@read2.cgocable.net...

"Mario Charest" <po********@127.0.0.1> wrote in message
news:Yy*********************@wagner.videotron.net. ..
.
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

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.
I'll try it out thanks.

Sean.

Jul 22 '05 #8

P: n/a
Seems to me like only being able to overload [] is an oversight. If you can
overload one [] you should be able to overload [][], oh well.

Thanks everyone.


I disagree with this. Can't you use arbitrarily nesting of [][][] in C++?

Then your suggestion implies that in order for a custom class to use
this syntax, you are required to have an overload per level.
n-dimension array requiring n overloaded operators?

Doesn't sound all that good.

I your deciion of using operator() instead of operator[] is the way to go.

Good luck.

Jorge L.
Jorge L.
Jul 22 '05 #9

P: n/a

"Jorge Rivera" <jo*****@rochester.rr.com> wrote in message
news:Vc*****************@twister.nyroc.rr.com...
Seems to me like only being able to overload [] is an oversight. If you can overload one [] you should be able to overload [][], oh well.

Thanks everyone.


I disagree with this. Can't you use arbitrarily nesting of [][][] in C++?

Then your suggestion implies that in order for a custom class to use
this syntax, you are required to have an overload per level.
n-dimension array requiring n overloaded operators?

Doesn't sound all that good.

I your deciion of using operator() instead of operator[] is the way to go.

Good luck.

Jorge L.
Jorge L.


You can also do it with clever use of templates. This avoids the need to
write one class for each dimension of your n-dimensional array. Instead you
have a template with a integer parameter representing the array dimension.

Look at the boost MultiArray class for an example,
http://www.boost.org/libs/multi_array/doc/index.html

john
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.