home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!crdgw1!rpi!uwm.edu!spool.mu.edu!agate!doc.ic.ac.uk!pipex!demon!trmphrst.demon.co.uk!nikki
- From: nikki@trmphrst.demon.co.uk (Nikki Locke)
- Newsgroups: comp.lang.c++
- Subject: Re: Question of scope
- Message-ID: <728158199snx@trmphrst.demon.co.uk>
- Date: 27 Jan 93 11:09:59 GMT
- References: <schmidt.33@rz.uni-passau.de> <1993Jan22.184558.21397@porthos.cc.bellcore.com>
- Sender: usenet@demon.co.uk
- Reply-To: nikki@trmphrst.demon.co.uk
- Organization: Trumphurst Ltd.
- Lines: 46
- X-Mailer: cppnews $Revision: 1.31 $
-
- In article <schmidt.33@rz.uni-passau.de> schmidt@rz.uni-passau.de (SCHMIDT GUIDO) writes:
- > In article <1993Jan22.184558.21397@porthos.cc.bellcore.com> chw@bellcore.com (Charlie Woloszynski,MRE 2J-278,8295228,,27143) writes:
- > >A (novice) question about scope.
- ..
- > >Anyway, does the following call the destructor just after returning
- > >from the foo?
- > >
- > > foo(& Derived1());
- > >
- > >Does it have any ugly portability problems?
- >
- > I've tried this in the following short program. It seems to work. At the end
- > of main() there are two different ways stated to do this. I don't know which
- > one is better. Any comments from others please.
-
- This appears to be rather a novice answer to the novice question :-)
-
- The fact that a particular program, compiled with a particular compiler,
- works is not a useful indication of whether the program is even correct,
- let alone portable !
-
- The original question relates to the lifetime of unnamed temporaries -
- when is the temporary Derived1 destroyed. Opinions about the lifetime of
- such a temporary vary widely - even the ANSI committee are wrestling with
- this problem.
-
- Having read the ARM (s 12.2, pp 267-270), I figure the construct is safe,
- because the ARM states : "If a reference is bound to a temporary, the
- temporary must not be destroyed until the reference is. This destruction
- must take place before exit from the scope in which the temporary is
- created."
-
- I figure the temporary can't be destroyed until return from foo. I could
- easily be wrong, though :-)
-
- Note, also, that some compilers (cfront, for example) have bugs in this
- area - an interesting program was posted here some weeks ago which
- demonstrated that cfront didn't even correctly match constructor and
- destructor calls (i.e. objects were constructed, but never destroyed). I
- was pleased to observe that this is one area where DOS compilers were
- better than cfront - all 3 of the DOS compilers I use handled the program
- correctly.
-
- --
- Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
- trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
-