-
June 25th, 2008, 11:17 PM
#1
[RESOLVED] Overloading [][]
As the title implies, I have a wrapper for a two dimensional array that I want to access by simple saying ColorCluster[X][Y]. But I can't figure out how to overload it. I came up with this idea
Code:
ColorCluster operator [] (int XY)
{
if (TempCol == 0)
{
TempCol = XY;
return *this;
}
else
{
int Buffer = TempCol;
TempCol = 0;
return Select (Buffer, XY);
}
};
But the Select(X,Y) function returns a boolean value. So this approach won't work. I'm sure this has been covered before but I can't seem to find a good article on it. Can anyone help me out?
-
June 25th, 2008, 11:41 PM
#2
Re: Overloading [][]
If you had done a search, you might have discovered [] operator overiding - a dumb question ?
That said, I still think that it is generally simpler to overload operator() instead, as the starter of that thread did originally.
-
June 25th, 2008, 11:48 PM
#3
Re: Overloading [][]
Thank you, sorry for asking an already covered question. I also may have come up with an alternate solution. I haven't been able to test it yet though.
Code:
Column operator [] (int X)
{
return Column (this, X);
}
class Column
{
private:
int Col;
ColorCluster* Master;
public:
Column(ColorCluster* CC, int X) {Master = CC; Col = X;}
bool operator [] (int Y) {return Master->Select(Col, Y);}
}
Again, sorry for asking an old question.
-
June 26th, 2008, 03:42 AM
#4
Re: Overloading [][]
That is the proxy solution mentioned in the other thread (Column is the proxy).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|