Path: utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!netcom!teda!ditka!mcdchg!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.std.c++ Subject: Re: Calling constructors for parms passed by value Message-ID: <1991Feb15.140522.11377@clear.com> Date: 15 Feb 91 14:05:22 GMT References: <631@necssd.NEC.COM> <70619@microsoft.UUCP> Organization: Clear Communications, Inc. Lines: 78 In article <70619@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: >In article <631@necssd.NEC.COM> harrison@necssd.NEC.COM (Mark Harrison) writes: >|A question arose in our internal C++ class. The instructor was >|describing a bug to look out for in some C++ compilers. The >|code was something like this: >| >|class bug { >| bug() { cout << "in constructor" } >| ~bug() { cout << "in destructor" } >|} >|main() >|{ >| bug a,b; >| foo(a,b); >|} >|foo(bug a, bug b) >|{ >| cout << "in foo"; >|} >| >|The output of a cfront based compiler was >| >|in constructor >|in constructor >|in foo >|in destructor >|in destructor >| >|While on some other compilers (TC++, Zortec, G++, versions unknown) the >|output was >| >|in constructor >|in constructor >|in foo >|in destructor <-- apparently destructed in foo() >|in destructor <-- apparently destructed in foo() >|in destructor >|in destructor >| >|We agreed that it was a bug that #constructors != #destructors, but had >|a disagreement about whether the number of constructor/destructor pairs >|should have been 2 or 4. >| >|So my question: Should a constructor/destructor be called for a formal >|parameter that has been passed by value? Some people said yes, because >|it is coming into/out of scope. Some people said no, because it had >|already been copied onto the stack and therefore existed. Our instructor >|said it was implementation defined, but I can't believe that this is >|true. Any references that anyone can give me? > >The appropriate reference would seem to be ARM, bottom of page 288: > >"The initialization that occurs in argument passing and function return is >equivalent to the form > > T x = a; " [which invokes a copy constructor not the default constructor] > >So, I'd claim in the situations where you appear to be getting more >destructors than constructors, in fact the compilers are doing the >right thing. > I would only add to this that the SUN compiler spit out the following warnings when I tried this program: "bug.cc", line 10: warning: a not used "bug.cc", line 10: warning: b not used "bug.cc", line 13: warning: no value returned from foo() This is in reference to the fact the 'foo' does not use the parameters. In this case it appears that the compiler chose not to construct them since they were not used. -- +-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for | | rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want | | uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all | +----------------------+:R::R::CCC:M:::::M:| the blame. So there. |