C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Overloading + Operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
- Steve -
Guest





PostPosted: Tue Jul 29, 2003 4:21 am    Post subject: Overloading + Operator Reply with quote



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::operator+(IntArray &addThis)
{
IntArray temp(arrayHigh-arrayLow);

for(int i=temp.low();i temp[i]=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?


Back to top
Alf P. Steinbach
Guest





PostPosted: Tue Jul 29, 2003 4:38 am    Post subject: Re: Overloading + Operator Reply with quote



On Tue, 29 Jul 2003 04:21:31 GMT, "- Steve -" <sevans (AT) foundation (DOT) sdsu.edu> wrote:

Quote:
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?



Quote:
I have overloaded the + operator like this.

IntArray& IntArray::operator+(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.


Quote:
{
IntArray temp(arrayHigh-arrayLow);

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



Quote:
for(int i=temp.low();i temp[i]=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.

Quote:
}

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.


Quote:
that does a delete array[].

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


Quote:
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.


Back to top
- Steve -
Guest





PostPosted: Tue Jul 29, 2003 4:45 am    Post subject: Re: Overloading + Operator Reply with quote



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" <alfps (AT) start (DOT) no> wrote

Quote:
On Tue, 29 Jul 2003 04:21:31 GMT, "- Steve -" wrote:

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::operator+(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[i]=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.




Back to top
John Harrison
Guest





PostPosted: Tue Jul 29, 2003 6:08 am    Post subject: Re: Overloading + Operator Reply with quote


"- Steve -" <sevans (AT) foundation (DOT) sdsu.edu> wrote

Quote:
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::operator+(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::operator+=(const IntArray &addThis)

john



Back to top
John Dibling
Guest





PostPosted: Tue Jul 29, 2003 4:11 pm    Post subject: Re: Overloading + Operator Reply with quote

On Tue, 29 Jul 2003 04:21:31 GMT, "- Steve -"
<sevans (AT) foundation (DOT) sdsu.edu> wrote:

Quote:
What should I do?


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

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

Back to top
Victor Bazarov
Guest





PostPosted: Tue Jul 29, 2003 4:18 pm    Post subject: Re: Overloading + Operator Reply with quote

"- Steve -" <sevans (AT) foundation (DOT) sdsu.edu> wrote...
Quote:
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:bg530u$4u9jl$1 (AT) ID-196037 (DOT) news.uni-berlin.de...


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::operator+=(const IntArray &addThis)

john

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



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Wed Jul 30, 2003 9:00 am    Post subject: Re: Overloading + Operator Reply with quote



John Dibling wrote:
Quote:

On Tue, 29 Jul 2003 04:21:31 GMT, "- Steve -"
[email]sevans (AT) foundation (DOT) sdsu.edu[/email]> wrote:

What should I do?


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

Please analyze the code better. He already does this:

Quote:
temp[i]=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:

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

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

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.