
-----------------------------------
copthesaint
Thu Mar 28, 2013 5:24 pm

Operator [][]?
-----------------------------------
Im currently making a class and I'm using operator class Foo{
private:
int * bar;
...
public:
...
int& operator

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 "...
public:
int& operator 

-----------------------------------
nullptr
Thu Mar 28, 2013 6:49 pm

Re: Operator [][]?
-----------------------------------
Overloading the 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
Thu Mar 28, 2013 8:02 pm

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
Fri Mar 29, 2013 8:06 am

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
Fri Mar 29, 2013 1:44 pm

Re: Operator [][]?
-----------------------------------

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 
I'm not super familiar with C++, but I know C# lets you overload 

What do you mean by overloading "Overloading the is no 

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
Fri Mar 29, 2013 2:54 pm

Re: Operator [][]?
-----------------------------------
Overloading the is no 

You should be able to overload, I don't think that it would be an issue, I know these two operators are different "
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
Fri Mar 29, 2013 3:09 pm

RE:Operator [][]?
-----------------------------------
If you access a multi-dimensional array with 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);[/code]

I dunno if this will actually compile, but it gets the idea across.

-----------------------------------
mirhagk
Fri Mar 29, 2013 6:57 pm

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.

-----------------------------------
copthesaint
Fri Mar 29, 2013 10:06 pm

Re: RE:Operator [][]?
-----------------------------------
Never mind, it looks like C++ doesn't support 

Thanks, that will be good to know for later when I start working on c#


If you access a multi-dimensional array with that array. Similarly, say you have an object that overloads the 

Its a good idea, however I am using a normal array. And how would I be able to pass the information from one operator class Foo{ 
private: 
int * bar; 
... 
public: 
... 
int* operator
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.


Overloading the is no 

You should be able to overload, I don't think that it would be an issue, I know these two operators are different "
nullptr is right there is no 

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
Sat Mar 30, 2013 2:20 pm

Re: Operator [][]?
-----------------------------------
There's no equivalent in C++ except using an at() function, like DemonWasp said.
