home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12479 < prev    next >
Encoding:
Text File  |  1992-08-17  |  1.7 KB  |  45 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: BCC: virtual function calls from constructor/destructor
  5. Message-ID: <1992Aug17.171730.17564@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Aug16.090135.6753@ennews.eas.asu.edu>
  8. Date: Mon, 17 Aug 1992 17:17:30 GMT
  9. Lines: 34
  10.  
  11. nwatson@enuxha.eas.asu.edu (Nathan F. Watson) writes:
  12.  
  13.  
  14. >I ran into a problem with virtual functions today when using my
  15. >new C++ compiler (Borland C++ 3.1).
  16.  
  17. >The scenario is as follows:
  18. >   * classes A and B are defined, and B is derived from A;
  19. >   * A's constructor calls a virtual function A::vfunc().  B::vfunc()
  20. >     is also defined.
  21.  
  22. >An unexpected (though perhaps not unreasonable) thing happens when I
  23. >try to construct an object of class B.  A::A() is called first, which
  24. >then calls the virtual function A::vfunc().  I would expect use of
  25. >B::vfunc().  However, A::A() calls A::vfunc().
  26.  
  27. That is exactly what is supposed to happen.
  28.  
  29. When you construct an object of type A, only A's members are available.
  30. No object of derived type yet exists, so no derived-class member
  31. functions could be called (without an explict cast).
  32.  
  33. A simple pragmatic explanation:  Suppose the derived-class function
  34. depends on derived-class data which is not yet available.  No good
  35. could come of calling that function from a base-class constructor!
  36.  
  37. The same rule applies to destructors.  During the destruction of an
  38. object, the derived classes are destroyed before the base classes.
  39. During execution of base-class destructors, no derived data or
  40. functions are available.
  41. -- 
  42.  
  43. Steve Clamage, TauMetric Corp, steve@taumet.com
  44. Vice Chair, ANSI C++ Committee, X3J16
  45.