From: Garrett S. <gs...@ex...> - 2002-06-20 21:06:31
|
Ioannis Vranos wrote: >Extremelly bad style code. > > > Hey! :p I realize it's not exactly tidy-ass code... I built the example to demonstrate the minimal requirement to show the difference. The code it it inherited from is somewhat different. > > >>int main() >>{ >> // (#1)produces errors >> ptr p = new alpha(); >> >> > > >In many compiles the initialisation upon definition, if possible, is >optimised out to a call to a constructor. For example: > >A x=r; is optimised to > >A x(r); > > Yes, That's what I assumed was supposed to happen. It's certainly what VC++ is doing, (which was easy enough to verify). I vaguely remember reading that was the desired behavior in this case. I'm still not sure why GCC doesn't bother doing that. Are there any methods to making GCC work in that fashion? I'm actually a little suprised that GCC doesn't do that. >instead of creating a temporary, then performing an assignment and then >destroying the temporary. > > Yeah, I assumed that is what it was trying to do. Hmmmm. > >Sorry for saying that, but the code is real garbage. > Again, it's not exactly designed to showcase code, just the effect. >It is produced because new alpha; (parenthesis are not needed) > Yeh! I realize that parenthesis aren't *required*, but it's simpler for the group of people who move back and forth between c++, c# and java to maintain a simple set of rules. (which have zero impact upon code generation). >can be converted to a >ptr temp object and then can be assigned to p, or by calling the >constructor in the above style. > > The code involved is really a distillation of a whack of code which provides reference-counted pointers. It's actual use is more like: { Ptr<alpha> p = new alpha(); //... } // when p goes out of scope, it auto-deletes the object (as long as it hasn't been assigned elsewhere). which is meant to replace : { alpha* p = new alpha(); //... delete p; } > > >You can bypass the error by writing > >ptr p; > >p= new alpha; > > > > Yeah, I realized that... in the example (#3) does just that. It just doesn't lend itself to as smooth replacement of traditional pointers. G |