Thread View: uw.dcs.general
3 messages
3 total messages
Started by Jason Morris
Wed, 16 Dec 1992 19:27
Operator Overloading problem
Author: Jason Morris
Date: Wed, 16 Dec 1992 19:27
Date: Wed, 16 Dec 1992 19:27
27 lines
1751 bytes
1751 bytes
I have a class, Matrix, and I want to overload the operators +,-,* etc.
Matrix has a private member Data, which is a pointer to some dynamically
allocated storage. Matrix() allocates this storage, and ~Matrix() frees it.
Fine so far.
Now I want to overload, say, operator+. The problem is that I want to take
lhs and rhs Matrix arguements, and return the answer in a third Matrix. I
can't pass in a reference to the third Matrix, as operator+ has to take only
two arguements. I can't just pass back a reference to a Matrix declared within
the operator+() function, as it goes out of scope when the function exits.
So I ended up with:
friend Matrix operator+(Matrix& lhs,Matrix& rhs);
And I just do "return <local Matrix>"
The problem now is that although I'm getting back a copy of the local
variable, not the original (scope is okay), when the operator+ function
returns the destructor is called for the local Matrix, thus deleting the free
store that the data is held in. As my copy has the same value for the pointer
Data, it is now pointing to a useless storage area.
Question is: How should I be doing this? Surely I don't have to use a
function add(Matrix lhs,Matrix rhs,Matrix answer)? That seems to render the
operator overloading pretty useless.
Jas.
Re: Operator Overloading problem
Author: Paul Johns
Date: Thu, 17 Dec 1992 21:40
Date: Thu, 17 Dec 1992 21:40
33 lines
1485 bytes
1485 bytes
Any time you have a class which contains a pointer to dynamically-allocated memory (as your class does), you WILL NEED an appropriate: Assignment operator Copy constructor Destructor You will likely also need a default constructor. Your symptoms are consistent with forgetting one or both of the assignment operator or copy constructor, depending on how the addition operator was used. (Forgetting the destructor results in a memory leak.) If you forget any of these, you'll end up with the kinds of problems you're seeing. Note that these problems can be silent: since the compiler provides you with these functions if you don't provide them, you won't get any sort of compiler error. What I think is happening in your case is that the compiler supplied assignment operator or copy constructor is just doing a memberwise copy of your object. Your addition function is creating the temporary and allocating memory for it. The pointer to this memory is copied from the temporary to the object to which you're assigning/copying rather than copying the memory. // Paul Johns
Re: Operator Overloading problem
Author: John MAX Skaller
Date: Sat, 19 Dec 1992 18:26
Date: Sat, 19 Dec 1992 18:26
41 lines
2213 bytes
2213 bytes
In article <1992Dec16.192756.13271@dcs.warwick.ac.uk> jas@dcs.warwick.ac.uk (Jason Morris) writes:
>
> I have a class, Matrix, and I want to overload the operators +,-,* etc.
>Matrix has a private member Data, which is a pointer to some dynamically
>allocated storage. Matrix() allocates this storage, and ~Matrix() frees it.
>Fine so far.
No. No good. You MUST also have a copy constructor and assignment
operator that copy the contents of Data, and delete and copy its contents,
respectively.
At least initially, forget about the inefficieny, fix that later.
This technique is called the 'Orthodox Idiom' by Coplien, it
ensures the objects are first class 'just like int'. Coplien
provides the full recipe, follow it slavishly.
>
> friend Matrix operator+(Matrix& lhs,Matrix& rhs);
friend Matrix operator+(const Matrix& lhs, const Matrix& rhs);
// ^^^^^
// if you want to pass it expressions
>
> And I just do "return <local Matrix>"
>
And now it works. To make it more efficient, there are
lots of methods, Coplien is filled with them. Some matrix
packages go so far as to just build a 'parse tree' of the
calculation rather than doing it straight away, to optimise
speed and memory use [defered binding]
However, you should be able to make your package
more efficient without having to rewrite existing working
client code (although you might have to recompile).
--
;----------------------------------------------------------------------
JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads