Thread: How to overload [][]

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    How to overload [][]

    Hello,
    I dont know how to overload [][] operator. I've tried but it's not working. This is what i have done so far.

    Code:
    #include <iostream.h>
    #include <assert.h>
    class array
    {
    	public:
    	int a[5][5];
    		 int &operator[][](int,int);
    
    };
    int &array::operator[][](int b,int c)
    {
    	assert(b>0 && c>0 && b<5 && c<5);
    	return a[b][c];
    }
    
    int main(void)
    {
     array A;
     cout<<A[4][3];
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Also tell me how to overload operator functions such that when i use A[5]; it should create an array of size 5 in class and when i use cout<<a[4] it should output the value of a[4]

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I dont know how to overload [][] operator.
    Read: How do I create a subscript operator for a Matrix class?

    Also tell me how to overload operator functions such that when i use A[5]; it should create an array of size 5 in class
    That's not possible. Just provide an appropriate constructor.

    when i use cout<<a[4] it should output the value of a[4]
    For that you should overload operator[].
    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

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    How to create const array in class??Also tell me why are we using reference while overloading subscript operators??I mean if we dont use subscript operators then also the task can be accomplished..then why?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Also tell me why are we using reference while overloading subscript operators?
    You need to return a reference so the element may be modified. Also, read: subscript operators often come in pairs

    I mean if we dont use subscript operators then also the task can be accomplished..then why?
    What do you mean? Of course you do not need to use subscript operators as you can use a regular function to perform the subscripting.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overload *
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 04:44 AM
  2. Addition Operator Overload
    By DarkDot in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2007, 03:43 PM
  3. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 03:03 PM
  4. Buffer Overload
    By xddxogm3 in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2003, 03:21 PM
  5. overload new and delete
    By Roaring_Tiger in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2003, 07:48 PM