473,725 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overload []

..
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
9 1615

"Mario Charest" <po********@127 .0.0.1> wrote in message
news:Yy******** *************@w agner.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

"Mario Charest" <po********@127 .0.0.1> wrote in message
news:Yy******** *************@w agner.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
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<i nt> > 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

"Mario Charest" <po********@127 .0.0.1> wrote in message
news:Yy******** *************@w agner.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
John Harrison wrote:

"Mario Charest" <po********@127 .0.0.1> wrote in message
news:Yy******** *************@w agner.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

"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******** *************@w agner.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

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

"Mario Charest" <po********@127 .0.0.1> wrote in message
news:Yy******** *************@w agner.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
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

"Jorge Rivera" <jo*****@roches ter.rr.com> wrote in message
news:Vc******** *********@twist er.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4338
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; } This overload work but I have a curiosty If I try to approch to the name or to the surname or to the dateofbirth in this way i receive error: ostream & operator<<(ostream &out, Person &p){ out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
1
2066
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this least operator for the class Person, but I don't know how write the overload for a class that derived from the class Person. The overload of << in Person is this: ostream & operator<<(ostream &out, const Persona &p){ out<<p.getNome()<<"...
3
2245
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside an array of pointer to Attore. I have written these overload in these ways. the overload of << work correctly the overload of >> I don't know. I compile the class correctly, when I insert a film through the operator >> I don't receive error, but...
4
1897
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement as a char rather than incorrectly representing it as an int. I've done this because I want to develop the class so that it will be able to convert between values, ie if I add 500 m to 1 km I'd like a correct result given in metres (1500 m in this...
17
2509
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
10
2218
by: shachar | last post by:
hi all. can i OverLoad Operators - such as Exclamation Mark "!" ? how?
9
2382
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I attempt to check to see if the object is null: "The call is ambiguous between the following methods or properties: 'TestObject.operator ==(TestObject, string)' and 'TestObject.operator ==(TestObject, TestObject)" Does anyone have any idea how to...
3
5820
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as long long int), a decimal (double), or a string (std::string). I basically want to overload most of a Message's operators so that the Message class feels more like a native type. For example... Message msg = "tester"; std::string str = msg;...
5
3695
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar explicitly takes a pointer-to-member with no parameters, why is the lookup for the overloaded function not able to use the number of arguments to determine the appropriate function? Is there a relevant portion of the standard that governs the...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.