CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2008
    Posts
    26

    [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?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2008
    Posts
    26

    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.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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
  •  





Click Here to Expand Forum to Full Width

Featured