2 Replies - 858 Views - Last Post: 24 November 2013 - 12:11 AM Rate Topic: -----

#1 cdosrun1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-October 13

A question about overloading the index [ ] operator

Posted 23 November 2013 - 11:00 PM

Hello all,

I found another idiosyncrasy in a small program I a writing. I am writing a Dynamic Array class that looks likes this:

class DynamicArray{
	friend ostream& operator<<(ostream&, DynamicArray&);
public:
	DynamicArray(const int& capacity=1) : array(new int[capacity]), capacity(capacity), size(0)
	{
		for(int i = 0; i < capacity; i++)
		{ 
			array[i] = NULL; 
		} 
	}  
	int& operator[](const int index){
                sizeOfArray(); 
		return array[index]; 
	}
	int sizeOfArray(){
		for(int i = 0; i < capacity; i++){
			if(array[i] != NULL){ size++; }
		}
		return size; 
	}

private:
	int* array; 
	int size; 
	int capacity; 
};




The problem occurs in the index operator. I added a call to the sizeOfArray function so that it updates everytime the user uses it. Example:

int main(){
   
   DynamicArray a(2); 
   a[0] = 1; // <- this should update the size of the array to 1

   return 0; 
}




This does not work. Instead I get an error resulting in the display going haywire and spitting out garbage code until the console crashes. Now, the curious thing about this is, if I remove the call to the function in the index operator overloading function, and leave the call before the for loop in the ostream overloading function, the output is correct. Does anyone know why?

Is This A Good Question/Topic? 0
  • +

Replies To: A question about overloading the index [ ] operator

#2 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: A question about overloading the index [ ] operator

Posted 23 November 2013 - 11:51 PM

Hi, sounds as if capacity is not updated properly, you could change the name of the parameter in the constructor to see if that makes a difference :

 DynamicArray(const int cap=1) 
       : array(new int[cap]), capacity(cap), size(0)


.

This post has been edited by #define: 23 November 2013 - 11:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: A question about overloading the index [ ] operator

Posted 24 November 2013 - 12:11 AM

$ g++ foo.cpp
foo.cpp: In constructor ‘DynamicArray::DynamicArray(const int&)’:
foo.cpp:11:18: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
foo.cpp: In member function ‘int DynamicArray::sizeOfArray()’:
foo.cpp:20:22: warning: NULL used in arithmetic [-Wpointer-arith]


First, fix your errors.
You have an array of ints, so use 0 to initialise the elements, not NULL.

The other thing is, the [] operator is only returning a reference to an element. It doesn't know whether it's being used in a 'read' context or a 'write' context.

> a[0] = 1; // <- this should update the size of the array to 1
If you did a[1] = 1, would that also be size = 1 (one used element) or size = 2 (max subscript seen so far)
Or something else?

Your use of size and capacity is really going to confuse you when you come across the STL, which uses these terms for different concepts to what you're trying to make them mean.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1