home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: BCC: virtual function calls from constructor/destructor
- Message-ID: <1992Aug17.171730.17564@taumet.com>
- Organization: TauMetric Corporation
- References: <1992Aug16.090135.6753@ennews.eas.asu.edu>
- Date: Mon, 17 Aug 1992 17:17:30 GMT
- Lines: 34
-
- nwatson@enuxha.eas.asu.edu (Nathan F. Watson) writes:
-
-
- >I ran into a problem with virtual functions today when using my
- >new C++ compiler (Borland C++ 3.1).
-
- >The scenario is as follows:
- > * classes A and B are defined, and B is derived from A;
- > * A's constructor calls a virtual function A::vfunc(). B::vfunc()
- > is also defined.
-
- >An unexpected (though perhaps not unreasonable) thing happens when I
- >try to construct an object of class B. A::A() is called first, which
- >then calls the virtual function A::vfunc(). I would expect use of
- >B::vfunc(). However, A::A() calls A::vfunc().
-
- That is exactly what is supposed to happen.
-
- When you construct an object of type A, only A's members are available.
- No object of derived type yet exists, so no derived-class member
- functions could be called (without an explict cast).
-
- A simple pragmatic explanation: Suppose the derived-class function
- depends on derived-class data which is not yet available. No good
- could come of calling that function from a base-class constructor!
-
- The same rule applies to destructors. During the destruction of an
- object, the derived classes are destroyed before the base classes.
- During execution of base-class destructors, no derived data or
- functions are available.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-