Strange Results with Overloaded Operators

J

John Harrison

- Steve - said:
I'm having problems with the code you can find at http://planetevans.com/c

Particulary with test6(). However even with the earlier tests they work
fine when run, but crash when run in the debugger. I'm also still having
problems with the destructor.

Both your operator[] are wrong. Think carefully about what happens when
arrayLow is not zero.

Should have spotted this in your earlier post, sorry.

john
 
S

- Steve -

I'm having problems with the code you can find at http://planetevans.com/c

Particulary with test6(). However even with the earlier tests they work
fine when run, but crash when run in the debugger. I'm also still having
problems with the destructor.
 
S

- Steve -

John Harrison said:
Both your operator[] are wrong. Think carefully about what happens when
arrayLow is not zero.

Should have spotted this in your earlier post, sorry.

john

Okay that was a stupid mistake. Changed them to execute return
array[forLocation];

I'm still having problems though. With the destructor enabled it crashes on
test2(). Without it enabled it crashes on test3().
 
E

ES Kim

- Steve - said:
John Harrison said:
Both your operator[] are wrong. Think carefully about what happens when
arrayLow is not zero.

Should have spotted this in your earlier post, sorry.

john

Okay that was a stupid mistake. Changed them to execute return
array[forLocation];

I'm still having problems though. With the destructor enabled it crashes on
test2(). Without it enabled it crashes on test3().

Still wrong.
What if a[-3] for IntArray a(-3, 6)?
 
E

ES Kim

- Steve - said:
Then it returns array[-3] which is a valid variable isn't it?

No. Array index starts from 0. You just changed the interface
using the array. It should be

int& IntArray::eek:perator[] (int forLocation)
{
return array[forLocation - arrayLow];
}

For a[-3], it must return array[0].
 
S

- Steve -

Then it returns array[-3] which is a valid variable isn't it?


ES Kim said:
- Steve - said:
John Harrison said:
Both your operator[] are wrong. Think carefully about what happens when
arrayLow is not zero.

Should have spotted this in your earlier post, sorry.

john

Okay that was a stupid mistake. Changed them to execute return
array[forLocation];

I'm still having problems though. With the destructor enabled it crashes on
test2(). Without it enabled it crashes on test3().

Still wrong.
What if a[-3] for IntArray a(-3, 6)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top