473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Overloading + Operator

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.high();++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?
Jul 19 '05 #1
6 3009
On Tue, 29 Jul 2003 04:21:31 GMT, "- Steve -" <se****@foundation.sdsu.edu> 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.high();++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.

Jul 19 '05 #2
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" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Tue, 29 Jul 2003 04:21:31 GMT, "- Steve -" <se****@foundation.sdsu.edu> wrote:
If you want to see all the code it's at http://planetevans.com/c However Ithink 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.high();++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 outof 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.

Jul 19 '05 #3

"- Steve -" <se****@foundation.sdsu.edu> wrote in message
news:fD*******************@news2.central.cox.net.. .
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
Jul 19 '05 #4
On Tue, 29 Jul 2003 04:21:31 GMT, "- Steve -"
<se****@foundation.sdsu.edu> wrote:
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
Jul 19 '05 #5
"- Steve -" <se****@foundation.sdsu.edu> wrote...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.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
Jul 19 '05 #6


John Dibling wrote:

On Tue, 29 Jul 2003 04:21:31 GMT, "- Steve -"
<se****@foundation.sdsu.edu> 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:
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:
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
kb******@gascad.at
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.