home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mole-end!mat
- From: mat@mole-end.matawan.nj.us
- Subject: Re: destruction of temporaries
- Message-ID: <1992Aug14.171659.14578@mole-end.matawan.nj.us>
- Summary: When to allow copy constructors
- Organization: :
- References: <46140@sunquest.UUCP> <rmartin.713716434@thor>
- Date: Fri, 14 Aug 1992 17:16:59 GMT
- Lines: 42
-
- In article <rmartin.713716434@thor>, rmartin@thor.Rational.COM (Bob Martin) writes:
- > bill@sunquest.UUCP (Bill Raves) writes:
-
- > Giving the compiler access to the copy semantics of a class is a
- > design decision which must be made carefully. It should not be done
- > in every case. Many so called "cannonical forms" recommend the
- > unquestioned inclusion of copy constructors and assigment operators.
- > I disagree with this approach. Copy semantics are often too complex
- > to be allowed into general use.
-
- > Whenever you provide a regular copy constructor, you are granting
- > permission to the compiler to make temporaries. This must be a
- > conscious choice, and must be supported by construction and
- > destruction semantics which are valid in any context.
-
- I'd like to offer some criteria to consider when making this decision.
-
- The principle question, IMO, is `What are the relationships that the
- object is involved with?'
-
- If the object is involved with problem-domain relationships wherein the
- object present in memory represents a cardinality-specific entity in the
- problem, you cannot use a naive copy constructor and may not be able to
- use any. (e.g. you can't allow random copies of the object that represents
- TheCEO, but you can allow any number of copies of the character array that
- represents the CEO's name.)
-
- If the object is involved with solution-domain relationships, and the
- related objects lie entirely within the object (e.g. the real and
- imaginary parts of a complex number) this relationship poses no
- obstacle to copy construction. But if the relationship involves objects
- or entities (e.g. resources) outside the object (e.g. a String class
- that allocates memory) either a special copy constructor is needed or
- else no copy constructor is sensible.
-
- Anyone who can express this more clearly and put it in a good textbook
- deserves to make some money off it.
- --
- (This man's opinions are his own.)
- From mole-end Mark Terribile
-
- mat@mole-end.matawan.nj.us, Somewhere in Matawan, NJ
-