how to overload [] operator?

This is a discussion on how to overload [] operator? within the C++ Programming forums, part of the General Programming Boards category; I don't understand how and why to overload the [ ] operator. Could someone please help me understand ?...

  1. #1
    Unregistered
    Guest

    how to overload [] operator?

    I don't understand how and why to overload the [ ] operator.
    Could someone please help me understand ?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Simple example, make a vector that has ranged checked indexing. This could be better done if it threw exceptions rather than exited when an out of bounds index was tried to be accessed, and if it was templated rather than just used for doubles. Nonetheless, it still illustrates the purpose of operator overloading. This new class will behave similair to an array, and the syntax for accessing elements is the same.
    Code:
    class doubleVec {
    public:
    	double& operator[](int index);
    	doubleVec(int numElements);
    	~doubleVec();
    private:
    	doubleVec(const &doubleVec) { } // disallow copying
    	double* array;
    	int size;
    };
    
    doubleVec::doubleVec(int numElements) {
    	array = new double[numElements];
    	size = numElements;
    }
    
    doubleVec::~doubleVec() {
    	delete[] array;
    }
    
    double&::operator[] (int index) {
    	if (index >= size) {
    		cerr << "Out of range, tried to access " << index << " max index is " << size - 1;
    	exit(1);
    	}
    	else if (index < 0) {
    		cerr << "Negative index of " << index;
    		exit(1);
    	}
    	else return array[index];
    }
    I haven't debugged that code, but it's just for show purposes.
    Last edited by SilentStrike; 10-17-2001 at 06:47 PM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest
    thanks for your reply, I appreciated it.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You should also overload the const and non-const operator[]'s as
    someone could write

    Vector A;

    A[4] = 4;

    and

    const Vector A;

    if (A[4] == 4) do_something();

    With out the const overloaded someone using your class
    could run into trouble.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 02:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 11:50 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM
  5. Overloading the [] operator trouble
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:46 PM

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