home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!wingnut!pauljo
- From: pauljo@microsoft.com (Paul Johns)
- Subject: Re: Can't I initialize a dynamically allocated object in its constructor?
- Message-ID: <1992Dec23.202751.29726@microsoft.com>
- Date: 23 Dec 92 20:27:51 GMT
- Organization: Microsoft Corp.: Redmond, Washington, USA
- References: <1992Dec22.234420.6610@microsoft.com> <1992Dec17.205113.1180@IRO.UMontreal.CA>
- Lines: 64
-
- I forgot to change the distribution to world in my last post, so it
- never got to the person who was asking. Mea culpa.
-
- I'm enclosing it verbatim below. Mea culpa for the repeat.
-
- I like the points the others have made, too!
-
- // Paul Johns
-
- In article <1992Dec22.234420.6610@microsoft.com> pauljo@microsoft.com wrote:
- > You've made common beginning mistake #1: not defining
- > your own copy constructor when one is needed.
- >
- > Your code WILL NOT WORK correctly until you provide a proper
- > copy constructor. The fact that it seems to work in one case
- > is not the whole story: it is actually failing in subtle
- > ways you're not looking at. And the initialization you ask
- > about is not the issue here.
- >
- > What's going on is that since you didn't provide a copy
- > constructor, the compiler provides one for you which simply
- > copies the members of your class one member at a time.
- >
- > But the data in the vector doesn't get copied--only the pointer.
- > And you end up deleting data twice since two different objects
- > point to it.
- >
- > This is really bad--it tends to mess up the heap something awful.
- >
- > There is at least one C++ book whose author tells nothing
- > of copy constructors. I hope you didn't use that one!!!
- >
- > Good texts (Lippman, Stroustrup, various Microsoft Press books,
- > etc.) do a good job of explaining copy constructors and why
- > they're needed. Check out the dynamic string classes, which
- > are very similar to your vector class.
- >
- > If your class needs an assignment operator, it will almost
- > certainly need a copy constructor and destructor as well.
- >
- > Two other points I noticed in your code:
- >
- > 1. You assignment operator doesn't check for doing "a = a;" Also,
- > it DOES make sense to assign vectors of different sizes,
- > you may want to allow that. Check how a good dynamic
- > string class works for ideas.
- >
- > 2. operator + is usually implemented as a file-scope (global)
- > function so that both operands can be converted, not
- > just the RHS operand. In your particular case, you
- > don't have any useful (meaning you'd want to write
- > something like "vecx = 5 + vecy;") conversions, so
- > this isn't an issue. You may want to rethink the
- > int constructor since it provides an implicit conversion
- > from int to vector, meaning that an int or any expression
- > that can be converted with a standard conversion to an
- > int can be used any place a vector can. (So "vec2 + 5"
- > **IS** legal for your class, although meaningless.)
- >
- > Good luck!
- >
- > // Paul Johns
-
-
-