Thread: Overloading Subscripting Operators?????

  1. #1
    Registered User
    Join Date
    Apr 2014
    Location
    Gebze
    Posts
    6

    Overloading Subscripting Operators?????

    I have two class GameOfLife and Cell and i want to overload square braket for class GameOfLife."if g is a GameOfLife object, g[10][5] will return the Cell at row 10 and column 5. If there is no such Cells, then it will return a new Cell with position (-1000,- 1000)."

    but if g[10][1000] and 1000>cols,then it returns different Cell exp (3,2)
    How i do control the col ( [row][col] )??? Sorry for my english.

    Code:
    vector<Cell> GameOfLife::operator [](const int row){
        vector<Cell> rowCell;
    
    
        for(int i=0; i<cols; ++i)
        {
            if( isLive(row,i)  )
                rowCell.push_back( Cell(row,i) );
            else
                rowCell.push_back( Cell(-1000,-1000) );
        }
    
    
        return rowCell;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you can overload operator[], but there is no operator[][] for you to overload. The typical workaround is to use a proxy object, i.e., you overload operator[] to return an object of a class that is implementation detail. This class will in turn have operator[] overloaded to return the "real" object that you want to return.

    Of course, this might seem more work than desirable just to get this syntactic sugar. If so, another approach is to cop out by saying: let's overload operator() to do the job instead, e.g., use g(10, 5) instead of g[10][5].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    The problem is that you can overload operator[], but there is no operator[][] for you to overload. The typical workaround is to use a proxy object, i.e., you overload operator[] to return an object of a class that is implementation detail. This class will in turn have operator[] overloaded to return the "real" object that you want to return.

    Of course, this might seem more work than desirable just to get this syntactic sugar. If so, another approach is to cop out by saying: let's overload operator() to do the job instead, e.g., use g(10, 5) instead of g[10][5].
    Idle question could you do it like g[10, 5] in C++?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by stahta01
    Idle question could you do it like g[10, 5] in C++?
    Yeah. It would be equivalent to g[5].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by stahta01 View Post
    Idle question could you do it like g[10, 5] in C++?

    Tim S.
    I presume you want x[n, m] to mean the same x[n][m]. If that's the case, then you can't do it directly with integers. You can do it with at least one non-integer type (i.e. a class), but it will result in ugly hacks.
    One method I can think of is basically x[index(n), m] where index's constructor basically stores n in a global variable which then x's operator can pick up. Ugly, hacky, but it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    unless you're already overloading the funciton-call operator with two int parameters, you can do something more like this:

    Code:
    Cell GameOfLife::operator ()(int row, int column)
    {
        // return the relevant cell here
    }
    This is what I do when I need what amounts to a two-dimensional subscript operator.

    On the other hand, why do you need to use operators? what's wrong with just having a GetCell(row, column) member function?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    On the other hand, why do you need to use operators? what's wrong with just having a GetCell(row, column) member function?
    Why not? It's used to make the syntax more natural. Why say vector.get(x, y) instead of vector(x, y) or vector[x][y]?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2014, 04:45 AM
  2. about overloading operators
    By joaquim in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2014, 10:41 AM
  3. Overloading operators
    By Shockey62 in forum C++ Programming
    Replies: 5
    Last Post: 04-23-2012, 11:23 AM
  4. help with overloading operators
    By ssjnamek in forum C++ Programming
    Replies: 9
    Last Post: 04-08-2011, 10:18 AM
  5. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM

Tags for this Thread