How to overload [][]

This is a discussion on How to overload [][] within the C++ Programming forums, part of the General Programming Boards category; Hello, I dont know how to overload [][] operator. I've tried but it's not working. This is what i have ...

  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
    22,114
    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[].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

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

    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, 05:44 AM
  2. Addition Operator Overload
    By DarkDot in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2007, 04: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, 04:21 PM
  5. overload new and delete
    By Roaring_Tiger in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2003, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21