home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!prism!federation!andy
- From: andy@federation.gatech.edu (Andy Register)
- Newsgroups: comp.lang.c++
- Subject: Re: Can't I initialize a dynamically allocated object in its constructor?
- Message-ID: <78548@hydra.gatech.EDU>
- Date: 18 Dec 92 00:50:52 GMT
- References: <1992Dec17.205113.1180@IRO.UMontreal.CA>
- Sender: news@prism.gatech.EDU
- Organization: CERL-EE, Georgia Institue of Technology
- Lines: 54
-
- In article <1992Dec17.205113.1180@IRO.UMontreal.CA> laviers@IRO.UMontreal.CA (Cocotte Minute) writes:
- >Hello,
- >
- >I have the following problem.
- >
- >a temporary vector is constructed to put the result of v1+v2 .
- >If there is no initialization in the constructor , it works just fine,
- >if there is initialization (here, to [-1,-1,-1...]), we seem to loose the value
- >of v1+v2... why???
- >
-
- You are almost there, you have failed to include a copy constructor
- that does a deep copy, i.e.
-
- Vector::Vector(const Vector& v);
- You have to allocate memory, assign values, blah, blah blah.
-
- Also, I like to have the following syntax for my operator=
-
- const Vector& Vector::operator=(const Vector& v);
-
- The first const means that folks cannot a=b=c=... and
- screw up assignment. Coplien or Meyers talks about this. You can
- also return a reference and I think it speeds things up.
-
- The second const and & emulate a fast pass by value. You don't
- have to create the temp to pass but you still cannot alter the
- passed in parameter (without heroic efforts on your part).
-
-
- >Pohl (and others) seem to think that it is good practice to initialize
- >objects... and have no undefined values.
-
- Yes I think this is a good idea but you cannot do it generically with
- templates.
- float DefaultValue() either is an undefined operation or
- results in an undefined floating point number I forget which.
-
- on the other hand
- Vector DefaultValue() can be well defined.
-
- You can see this kind of thing when you use templates. If anybody is
- listening, am I correct on this point?
-
- >is this a bug in the compiler?
-
- No.
-
- >Thank you very much for any help you can give me!
-
- welcome
-
- Toodles
- Andy
-