home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 20078 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  2.6 KB

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