🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: uw.dcs.general
Article #3

Re: Operator Overloading problem

#3
From: John MAX Skaller
Date: Sat, 19 Dec 1992 18:26
41 lines
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 ------------------
Technical Details
Message-ID: <1992Dec19.182648.11687@ucc.su.OZ.AU>
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
References: <1992Dec16.192756.13271@dcs.warwick.ac.uk>

Show Full Headers
Complete Article Headers
Newsgroups: uw.dcs.general,comp.lang.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Subject: Re: Operator Overloading problem
Message-ID: <1992Dec19.182648.11687@ucc.su.OZ.AU>
Sender: news@ucc.su.OZ.AU
Nntp-Posting-Host: extro.ucc.su.oz.au
Organization: MAXTAL P/L C/- University Computing Centre, Sydney
References: <1992Dec16.192756.13271@dcs.warwick.ac.uk>
Date: Sat, 19 Dec 1992 18:26:48 GMT
Lines: 41