Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Operator [][]?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
copthesaint




PostPosted: Thu Mar 28, 2013 5:24 pm   Post subject: Operator [][]?

Im currently making a class and I'm using operator [] to make it easier to access the values outside of the class.
Example:

c++:
class Foo{
private:
int * bar;
...
public:
...
int& operator[] (const int&);

}

int& Foo:operator[] (const int& index){
return bar[index];
}


This works fine for me, I don't believe there is an issue with it (*please correct me if I'm wrong) , but I as well would like to make a second operator overload to "[][]". If that is at all possible , how may I do this?
I tried this but I only had gotten syntax errors:

c++:
...
public:
int& operator [][] (const int&, const int&)
...
int& Foo:operator[][] (const int& indexX, const int& indexY){
return bar[(indexY - 1) * width + (indexX - 1)];
}
Sponsor
Sponsor
Sponsor
sponsor
nullptr




PostPosted: Thu Mar 28, 2013 6:49 pm   Post subject: Re: Operator [][]?

Overloading the [] operator is fine, though there is no need to declare the index as a const int& -- just use an int. But overloading the [][] operator doesn't work because there is no [][] operator. When you use a two-dimensional array like an int** and reference it with [][], you're simply applying the [] operator twice (once to get an int* and then again to get an int). For your class, depending on your purpose, you could make it return a pointer (which you could reference with []) or another instance of Foo, depending on your purpose. Hope that helps!
DemonWasp




PostPosted: Thu Mar 28, 2013 8:02 pm   Post subject: RE:Operator [][]?

The other common solution to this problem is to just use a method with a short name, like "int& Foo::at(int x, int y)" .
mirhagk




PostPosted: Fri Mar 29, 2013 8:06 am   Post subject: RE:Operator [][]?

I'm not super familiar with C++, but I know C# lets you overload [,], perhaps C++ does the same? Or does it not support multi-dimensional non-jagged arrays.
copthesaint




PostPosted: Fri Mar 29, 2013 1:44 pm   Post subject: Re: Operator [][]?

DemonWasp wrote:

The other common solution to this problem is to just use a method with a short name, like "int& Fooat(int x, int y)" .

That's what I was already doing, it just would look nicer if I can just overload [][], less typing and looks cleaner than the current method I can call.

mirhagk wrote:

I'm not super familiar with C++, but I know C# lets you overload [,], perhaps C++ does the same? Or does it not support multi-dimensional non-jagged arrays.


What do you mean by overloading "[,]," I'd be curious to know what you do in c#, I don't know if the same applies for c++ though.

nullptr @ Thu Mar 28, 2013 wrote:
Overloading the [] operator is fine, though there is no need to declare the index as a const int& -- just use an int. But overloading the [][] operator doesn't work because there is no [][] operator. When you use a two-dimensional array like an int** and reference it with [][], you're simply applying the [] operator twice (once to get an int* and then again to get an int). For your class, depending on your purpose, you could make it return a pointer (which you could reference with []) or another instance of Foo, depending on your purpose. Hope that helps!


You should be able to overload, I don't think that it would be an issue, I know these two operators are different "[]" and "[][]" however the compiler can already distinguished between "=" and "==" so why can't it tell the difference between these? The only difference is that "[][]" would take two arguments. Also it's not that there is a need, but because it would make code cleaner and easier to write. Plus there would be less overhead than having to call two methods.
Dreadnought




PostPosted: Fri Mar 29, 2013 2:54 pm   Post subject: Re: Operator [][]?

copthesaint wrote:
nullptr @ Thu Mar 28, 2013 wrote:
Overloading the [] operator is fine, though there is no need to declare the index as a const int& -- just use an int. But overloading the [][] operator doesn't work because there is no [][] operator. When you use a two-dimensional array like an int** and reference it with [][], you're simply applying the [] operator twice (once to get an int* and then again to get an int). For your class, depending on your purpose, you could make it return a pointer (which you could reference with []) or another instance of Foo, depending on your purpose. Hope that helps!


You should be able to overload, I don't think that it would be an issue, I know these two operators are different "[]" and "[][]" however the compiler can already distinguished between "=" and "==" so why can't it tell the difference between these? The only difference is that "[][]" would take two arguments. Also it's not that there is a need, but because it would make code cleaner and easier to write. Plus there would be less overhead than having to call two methods.

nullptr is right there is no [][] operator.

It's not that the compiler can't tell the difference between [] and [][], it's just that it treats them exactly the same way, just like * vs. **.

It you wanted, you could have a 4 dimensional array and access it with [][][][]. It's just an operator that is being applied multiple times, not a different operator. If it were a different operator it would have to be defined for arrays of any dimension, that is [], [][], [][][], [][][][], [][][][][], .... But C++ doesn't have infinitely many operators.
Insectoid




PostPosted: Fri Mar 29, 2013 3:09 pm   Post subject: RE:Operator [][]?

If you access a multi-dimensional array with [], an array is returned. You can then apply the [] operator again to return an element in that array. Similarly, say you have an object that overloads the [] operator. If you build an array of that object, then calling [] on it will return an object. If you call [][] on it, you will return whatever the [] operator returns for that object. It makes more sense if you apply dot notation.

code:
int foo[][];
return foo.[](2).[](3);


I dunno if this will actually compile, but it gets the idea across.
mirhagk




PostPosted: Fri Mar 29, 2013 6:57 pm   Post subject: RE:Operator [][]?

Never mind, it looks like C++ doesn't support [0,1] as array access it only has jagged array access (well it usually optimizes to the same thing, but you can't access it like a rectangular array)

C# supports accessing and overriding rectangular arrays which work like [,] instead of [][]. The difference is usually not important, especially with optimization, but for operator overloading it makes a difference.
Sponsor
Sponsor
Sponsor
sponsor
copthesaint




PostPosted: Fri Mar 29, 2013 10:06 pm   Post subject: Re: RE:Operator [][]?

mirhagk @ Fri Mar 29, 2013 wrote:
Never mind, it looks like C++ doesn't support [0,1] as array access it only has jagged array access (well it usually optimizes to the same thing, but you can't access it like a rectangular array)

C# supports accessing and overriding rectangular arrays which work like [,] instead of [][]. The difference is usually not important, especially with optimization, but for operator overloading it makes a difference.


Thanks, that will be good to know for later when I start working on c#

Insectoid wrote:

If you access a multi-dimensional array with [], an array is returned. You can then apply the [] operator again to return an element in that array. Similarly, say you have an object that overloads the [] operator. If you build an array of that object, then calling [] on it will return an object. If you call [][] on it, you will return whatever the [] operator returns for that object. It makes more sense if you apply dot notation.

code:
int foo[][];
return foo.[](2).[](3);


I dunno if this will actually compile, but it gets the idea across.


Its a good idea, however I am using a normal array. And how would I be able to pass the information from one operator [] to the next while still taking an arguement in the next operator [].
Example:
c++:
class Foo{
private:
int * bar;
...
public:
...
int* operator[] (const int&);
int& operator[] (const int&, const int*);
}

int* Foo:operator[] (const int& index){
int* tempBar;
//Figure out array upper bound value of tempBar
//Copy parts of array into tempBar
...
return tempBar;
}
int& Foo:operator[] (const int& index,const int* arrayRef){
int val;
val = arrayRef[index];
delete [] arrayRef;
return val
}

This is just a hypothetical, I don't think this works, but my point is how would I pass the array Reference? If I could then it would achieve the desired syntax, however, this example would also produce a fair amount of overhead for what it is doing.

Dreadnought wrote:

copthesaint wrote:
nullptr @ Thu Mar 28, 2013 wrote:
Overloading the [] operator is fine, though there is no need to declare the index as a const int& -- just use an int. But overloading the [][] operator doesn't work because there is no [][] operator. When you use a two-dimensional array like an int** and reference it with [][], you're simply applying the [] operator twice (once to get an int* and then again to get an int). For your class, depending on your purpose, you could make it return a pointer (which you could reference with []) or another instance of Foo, depending on your purpose. Hope that helps!


You should be able to overload, I don't think that it would be an issue, I know these two operators are different "[]" and "[][]" however the compiler can already distinguished between "=" and "==" so why can't it tell the difference between these? The only difference is that "[][]" would take two arguments. Also it's not that there is a need, but because it would make code cleaner and easier to write. Plus there would be less overhead than having to call two methods.

nullptr is right there is no [][] operator.

It's not that the compiler can't tell the difference between [] and [][], it's just that it treats them exactly the same way, just like * vs. **.

It you wanted, you could have a 4 dimensional array and access it with [][][][]. It's just an operator that is being applied multiple times, not a different operator. If it were a different operator it would have to be defined for arrays of any dimension, that is [], [][], [][][], [][][][], [][][][][], .... But C++ doesn't have infinitely many operators.


Thanks, I understand, however as stated by mirhagk, in c# you can do something like override [] to accept 2 parameters. Is there no equivalent in c++?



Thanks for explaining things everyone, I appreciate the help.[/code]
nullptr




PostPosted: Sat Mar 30, 2013 2:20 pm   Post subject: Re: Operator [][]?

There's no equivalent in C++ except using an at() function, like DemonWasp said.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: