Overloading + Operator

S

- Steve -

If you want to see all the code it's at http://planetevans.com/c However I
think I have all the relevant parts here.

main() makes the following call

IntArray c = a + b; // IntArray is my class

I have overloaded the + operator like this.

IntArray& IntArray::eek:perator+(IntArray &addThis)
{
IntArray temp(arrayHigh-arrayLow);

for(int i=temp.low();i<temp.high();++i)
temp=array[arrayLow+i]+addThis.array[addThis.arrayLow+i];

return temp;
}

However it doesn't appear that when the c= part of the line executes that
the temp array is following it. I assume that is becuase temp follows out
of scope and the destructor is called, that does a delete array[]. What
should I do?
 
A

Alf P. Steinbach

If you want to see all the code it's at http://planetevans.com/c However I
think I have all the relevant parts here.

main() makes the following call

IntArray c = a + b; // IntArray is my class

The critical thing here is the design, namely

What should happen if a and b have different array bounds?
- Notion of infinite array "extension"?
- Exception in all cases?
- Exception only if _sizes_ are different?


I have overloaded the + operator like this.

IntArray& IntArray::eek:perator+(IntArray &addThis)

This is a member function, conceptually for adding something
_into_ an existing array, which is modified by that operation.

That doesn't correspond to the usual semantics of '+'.

Instead, call this member function 'add', and use it as just a
helper in operator '+'.

Let operator '+' be a free-standing function, and let it be a
'friend' of IntArray if necessary.

{
IntArray temp(arrayHigh-arrayLow);

See the comment above about the design, which you should
resolve first of all.


for(int i=temp.low();i<temp.high();++i)
temp=array[arrayLow+i]+addThis.array[addThis.arrayLow+i];

return temp;


Here the code returns a reference to a variable 'temp' that is destroyed
by the time control goes back to the caller.
}

However it doesn't appear that when the c= part of the line executes that
the temp array is following it. I assume that is becuase temp follows out
of scope and the destructor is called
Yes.


that does a delete array[].

Probably, if that's what you have in the destructor.

What should I do?

See above. Summary: rename your current operator to 'add', let the return
type of that be either 'void' or else return reference to '*this', add a
friend operator '+' of two arguments which returns IntArray by value, but
first of all and most important, resolve the design question noted above.
 
S

- Steve -

Well that's another problem I have to deal with. Right now if a IntArray
object is created with no arguments then it creates an array going from 0 to
9. So that's what is happening here I assume becuase there is no arguments
on the decleration.

if "a" and "b" only need to be of the same size array. So if a was 1 to 4,
and b was 5 to 6. Then it would go a[1] + b[5], a[2] + b[6], etc, etc.

Steve


Alf P. Steinbach said:
If you want to see all the code it's at http://planetevans.com/c However I
think I have all the relevant parts here.

main() makes the following call

IntArray c = a + b; // IntArray is my class

The critical thing here is the design, namely

What should happen if a and b have different array bounds?
- Notion of infinite array "extension"?
- Exception in all cases?
- Exception only if _sizes_ are different?


I have overloaded the + operator like this.

IntArray& IntArray::eek:perator+(IntArray &addThis)

This is a member function, conceptually for adding something
_into_ an existing array, which is modified by that operation.

That doesn't correspond to the usual semantics of '+'.

Instead, call this member function 'add', and use it as just a
helper in operator '+'.

Let operator '+' be a free-standing function, and let it be a
'friend' of IntArray if necessary.

{
IntArray temp(arrayHigh-arrayLow);

See the comment above about the design, which you should
resolve first of all.


for(int i=temp.low();i<temp.high();++i)
temp=array[arrayLow+i]+addThis.array[addThis.arrayLow+i];

return temp;


Here the code returns a reference to a variable 'temp' that is destroyed
by the time control goes back to the caller.
}

However it doesn't appear that when the c= part of the line executes that
the temp array is following it. I assume that is becuase temp follows out
of scope and the destructor is called
Yes.


that does a delete array[].

Probably, if that's what you have in the destructor.

What should I do?

See above. Summary: rename your current operator to 'add', let the return
type of that be either 'void' or else return reference to '*this', add a
friend operator '+' of two arguments which returns IntArray by value, but
first of all and most important, resolve the design question noted above.
 
J

John Harrison

- Steve - said:
If you want to see all the code it's at http://planetevans.com/c However I
think I have all the relevant parts here.

main() makes the following call

IntArray c = a + b; // IntArray is my class

I have overloaded the + operator like this.

IntArray& IntArray::eek:perator+(IntArray &addThis)

The usual way to overload operator+ is as a two parameter non-member
function, i.e.

friend IntArray operator+(const IntArray& x, const IntArray& y);

This adds two arrays and returns a third array (note the return type is not
a reference).

What you have written is more like operator+= i.e. add something to an
existing array, return a reference to that existing array (i.e. return
*this).

Decide which you want first of all.

Also you missed const, on addThis

IntArray& IntArray::eek:perator+=(const IntArray &addThis)

john
 
J

John Dibling

What should I do?

You should make IntArrya::eek:perator+() operate on the 'this' pointer
rather than a temporary variable.

</dib>
John Dibling
Witty banter omitted for your protection
 
V

Victor Bazarov

- Steve - said:
I tried adding friend IntArray operator+(const IntArray& x, const IntArray&
y) as a function of the class, and outside of the class. Either way at
complie time I'm told 'operator`+'' : a friend function can only be declared
in a class.

Of course. The declaration statement John posted (the one with the
'friend' keyword in it) should go inside the IntArray class definition.
You still should define the function outside (and that definition
should not contain the 'friend' keyword because it makes no sense not
within a class). What does your book say about 'friend' specifier?

You don't expect those who respond to your posts to tie your shoes
for you as well, do you?

Victor
 
K

Karl Heinz Buchegger

John said:
You should make IntArrya::eek:perator+() operate on the 'this' pointer
rather than a temporary variable.

Please analyze the code better. He already does this:
temp=array[arrayLow+i]+addThis.array[addThis.arrayLow+i];


array is a member variable to the IntArray object. Granted: some
whitespace characters would have made this more obvious:
for( int i = temp.low(); i < temp.high(); ++i )
temp = array[ arrayLow+i ] +
addThis.array[ addThis.arrayLow+i ];


The OP's problem is that he returned a reference, when he should
return an object.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,220
Latest member
MathewSant

Latest Threads

Top