home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13668 < prev    next >
Encoding:
Text File  |  1992-09-14  |  2.0 KB  |  67 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!spool.mu.edu!agate!tfs.com!tfs.com!eric
  3. From: eric@tfs.com (Eric Smith)
  4. Subject: Re: Problems with constructor in C++
  5. Message-ID: <1992Sep15.083100.17828@tfs.com>
  6. Organization: TFS
  7. References: <1992Sep14.225041.15692@access.usask.ca> <1992Sep15.030705.13864@sunb10.cs.uiuc.edu>
  8. Distribution: na
  9. Date: Tue, 15 Sep 1992 08:31:00 GMT
  10. Lines: 55
  11.  
  12. In article <1992Sep15.030705.13864@sunb10.cs.uiuc.edu> pjl@sparc10.cs.uiuc.edu (Paul Lucas) writes:
  13. >In <1992Sep14.225041.15692@access.usask.ca> srikant@skorpio.usask.ca (Srikant Subramaniam) writes:
  14. >
  15. >>Hello Netters:
  16. >
  17. >>I have a rather peculiar problem with constructors in C++. In the code
  18. >>fragment below, the constructor for AtomicInt is not executed at all.
  19. >
  20. >>File chore.h
  21. >>------------
  22. >
  23. >>Class Chore : public DLINK {
  24. >
  25. >>    ......
  26. >>    .......
  27. >
  28. >>    AtomicInt numworkers;
  29. >>public: Chore ( .......) {
  30. >>    
  31. >>    }
  32. >>} // Class Chore
  33. >
  34. >>File atomic_int.h
  35. >>------------------
  36. >
  37. >>Class AtomicInt {
  38. >
  39. >>    ...........
  40. >
  41. >>    AtomicInt() { .......}
  42. >>    AtomicInt(int x) { ......}
  43. >>    AtomicInt(AtomicInt& x) { .......}
  44. >>}
  45. >
  46. >>When the code is executed, the constructor for AtomicInt doesn't get executed
  47. >>at all. My understanding of C++ was that when the initialization is done as
  48. >>in AtomicInt numworkers, the constructor is automatically executed. I hope I'm
  49. >>not wrong :-(
  50. >
  51. >>Could somebody out in net.land  clarify this ?
  52. >
  53. >*****>    You are correct.  The default constructor will (should) be called.
  54. >    (BTW, I assume you meant "class" not "Class"?)  Why doesn't it
  55. >    work then....don't know.
  56. >-- 
  57. >    - Paul J. Lucas                University of Illinois    
  58. >      AT&T Bell Laboratories        at Urbana-Champaign
  59. >      Naperville, IL            pjl@cs.uiuc.edu
  60.  
  61.  
  62. Maybe no Chore object ever gets constructed, because no Chore objects
  63. are declared or allocated?  And maybe AtomicInt is only used in
  64. Chore.  Thus it would be correct for no constructors to be invoked.
  65. Unless we see the actual code, we can only guess.  (And the typos prove
  66. this isn't the actual code.)
  67.