home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19922 < prev    next >
Encoding:
Text File  |  1993-01-27  |  3.2 KB  |  75 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!stripe!rmartin
  3. From: rmartin@stripe.Rational.COM (Bob Martin)
  4. Subject: Re: Virtual function call within constructors
  5. Message-ID: <rmartin.728088421@stripe>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <1993Jan24.051330.3721@dcc.uchile.cl> <1993Jan24.164709.18400@taumet.com>
  9. Date: Tue, 26 Jan 1993 22:47:01 GMT
  10. Lines: 63
  11.  
  12. steve@taumet.com (Steve Clamage) writes:
  13.  
  14. >gschwarz@dcc.uchile.cl (Guillermo Schwarz Utrup) writes:
  15.  
  16. >To: gschwarz@dcc.uchile.cl
  17. >Subject: Re: Virtual function call within constructors
  18. >Newsgroups: comp.lang.c++
  19. >References: <1993Jan24.051330.3721@dcc.uchile.cl>
  20.  
  21. >In comp.lang.c++ you write:
  22.  
  23. >>I have found that virtual functions are not handled as one could expect
  24. >>when called from a constructor.
  25.  
  26. >That is because you are not expecting the right thing.  Look up
  27. >virtual functions in constructors in any good C++ text.
  28.  
  29. >When constructing a base class, only functions visible from that class
  30. >constructor can be called.  The derived class has not been constructed yet,
  31. >so you can't call any of its functions.  For example, the derived version
  32. >of the virtual function might depend on data set up by the derived
  33. >constructor, which won't have yet been called.
  34.  
  35. Let me add to Steve's excellent explanation by talking about the
  36. dangers of calling pure virtual functions from within constructors and
  37. destructors.  
  38.  
  39. When an object is being constructed, its constructors are called in
  40. the order of inheritance.  The process starts by calling the
  41. constructor of the base-most class and ends by calling the constructor
  42. of the most derived class.  Now it is one of the jobs of the
  43. constructor to set the virtual table.  (This is implementation
  44. dependent, but the concept and results should be the same in all
  45. implementation sof the compiler)  Thus, as each constructor is called,
  46. the virtual table is changed to correpspond the the class of the
  47. currently executing constructor.  This is why, when you call a virtual
  48. function from a constructor or destructor it will never deploy any
  49. deeper than the class of the currently execuring constructor or
  50. destructor.
  51.  
  52. Now, what if you call a virtual function which, at the current level,
  53. is declared as a pure virtual.  Since the entry in the virtual table
  54. is undefined, the result of the call will be undefined.  (This
  55. typically means some kind of nasty crash).
  56.  
  57. Many compilers will prevent you from calling a pure virtual function
  58. from a constructor or a destructor, but there are ways around this.
  59. It is possible, from within a constructor or destructor, to call a
  60. function which calls a pure virtual function.  Most compilers cannot
  61. detect that.
  62.  
  63. So be very careful in your constructors and destructors.  Since
  64. virtual functions don't deploy, they should not be called.  If calling
  65. them is necessary then use the scope operator to call them directly:
  66. (e.g. MyClass::MyVirtualFunction()).  Never call a pure virtual
  67. function from a constructor or a destructor.....
  68.  
  69.  
  70. --
  71. Robert Martin       | Design Consulting    | Training courses offered:
  72. R. C. M. Consulting | rmartin@rational.com |  Object Oriented Analysis
  73. 2080 Cranbrook Rd.  | Tel: (708) 918-1004  |  Object Oriented Design
  74. Green Oaks, Il 60048| Fax: (708) 918-1023  |  C++
  75.