home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!rational.com!stripe!rmartin
- From: rmartin@stripe.Rational.COM (Bob Martin)
- Subject: Re: Virtual function call within constructors
- Message-ID: <rmartin.728088421@stripe>
- Sender: news@rational.com
- Organization: Rational
- References: <1993Jan24.051330.3721@dcc.uchile.cl> <1993Jan24.164709.18400@taumet.com>
- Date: Tue, 26 Jan 1993 22:47:01 GMT
- Lines: 63
-
- steve@taumet.com (Steve Clamage) writes:
-
- >gschwarz@dcc.uchile.cl (Guillermo Schwarz Utrup) writes:
-
- >To: gschwarz@dcc.uchile.cl
- >Subject: Re: Virtual function call within constructors
- >Newsgroups: comp.lang.c++
- >References: <1993Jan24.051330.3721@dcc.uchile.cl>
-
- >In comp.lang.c++ you write:
-
- >>I have found that virtual functions are not handled as one could expect
- >>when called from a constructor.
-
- >That is because you are not expecting the right thing. Look up
- >virtual functions in constructors in any good C++ text.
-
- >When constructing a base class, only functions visible from that class
- >constructor can be called. The derived class has not been constructed yet,
- >so you can't call any of its functions. For example, the derived version
- >of the virtual function might depend on data set up by the derived
- >constructor, which won't have yet been called.
-
- Let me add to Steve's excellent explanation by talking about the
- dangers of calling pure virtual functions from within constructors and
- destructors.
-
- When an object is being constructed, its constructors are called in
- the order of inheritance. The process starts by calling the
- constructor of the base-most class and ends by calling the constructor
- of the most derived class. Now it is one of the jobs of the
- constructor to set the virtual table. (This is implementation
- dependent, but the concept and results should be the same in all
- implementation sof the compiler) Thus, as each constructor is called,
- the virtual table is changed to correpspond the the class of the
- currently executing constructor. This is why, when you call a virtual
- function from a constructor or destructor it will never deploy any
- deeper than the class of the currently execuring constructor or
- destructor.
-
- Now, what if you call a virtual function which, at the current level,
- is declared as a pure virtual. Since the entry in the virtual table
- is undefined, the result of the call will be undefined. (This
- typically means some kind of nasty crash).
-
- Many compilers will prevent you from calling a pure virtual function
- from a constructor or a destructor, but there are ways around this.
- It is possible, from within a constructor or destructor, to call a
- function which calls a pure virtual function. Most compilers cannot
- detect that.
-
- So be very careful in your constructors and destructors. Since
- virtual functions don't deploy, they should not be called. If calling
- them is necessary then use the scope operator to call them directly:
- (e.g. MyClass::MyVirtualFunction()). Never call a pure virtual
- function from a constructor or a destructor.....
-
-
- --
- Robert Martin | Design Consulting | Training courses offered:
- R. C. M. Consulting | rmartin@rational.com | Object Oriented Analysis
- 2080 Cranbrook Rd. | Tel: (708) 918-1004 | Object Oriented Design
- Green Oaks, Il 60048| Fax: (708) 918-1023 | C++
-