🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #810

Calling constructors for parms passed by value

#810
From: Mark Harrison
Date: Fri, 01 Feb 1991 16:38
54 lines
1859 bytes
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?

Thanks in advance,
Mark.
-- 
Mark Harrison             harrison@necssd.NEC.COM
(214)518-5050             {necntc, cs.utexas.edu}!necssd!harrison
standard disclaimers apply...
Technical Details
Message-ID: <631@necssd.NEC.COM>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!texsun!smunews!necssd!harrison

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!texsun!smunews!necssd!harrison
From: harrison@necssd.NEC.COM (Mark Harrison)
Newsgroups: comp.std.c++
Subject: Calling constructors for parms passed by value
Message-ID: <631@necssd.NEC.COM>
Date: 1 Feb 91 16:38:42 GMT
Organization: NEC America Inc. SSD, Irving, TX
Lines: 54