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

  1. Path: sparky!uunet!airgun!ep130.wg2.waii.com!ep130!bab
  2. From: bab@se39.wg2.waii.com (Brian Button)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is this C++ advice from an OO book true? (beginner question)
  5. Message-ID: <BAB.93Jan24100815@se39.wg2.waii.com>
  6. Date: 24 Jan 93 15:08:15 GMT
  7. References: <sherman.727849469@foster> <dak.727870302@tabaqui>
  8. Organization: Western Geophysical Exploration Products
  9. Lines: 70
  10. NNTP-Posting-Host: se39.wg2.waii.com
  11. In-reply-to: dak@tabaqui.informatik.rwth-aachen.de's message of 24 Jan 93 10:11:42 GMT
  12.  
  13.  
  14. -->"David" == David Kastrup <dak@tabaqui.informatik.rwth-aachen.de> writes:
  15.  
  16. sherman@unx.sas.com (Chris Sherman) writes:
  17.  
  18. >I don't understand one of the book's recommendations:
  19.  
  20. >  11.  Call virtual class constructors in dervied class constructors.
  21.  
  22. >     I pity the poor fools who have to spend nights and weekends
  23. >     searching for the reason that a class variable occasionally
  24. >     takes on strange, unexpected values .  .  .  only to find
  25. >     through their bleary eyes that they forgot to allocate space for
  26. >     a virtual base (super) class, so that storage is allocated for
  27. >     the base class's members.  Even if the class has no members,
  28. >     declare it--you never know what future changes will come back to
  29. >     haunt you (or the person who inherits your mine field)!
  30.  
  31.  
  32. >Or if this is very good advice, could someone give an example showing
  33. >why it is true?
  34.  
  35. >This is a beginner question obviously--thanx for any help!
  36.  
  37. David> And the above book writes a beginner's answer. Constructors cannot be
  38. David> virtual and I pity the poor fools who have to spend nights and weekends
  39. David> only to find out that in C++ the type system is static, and so every
  40. David> object's type is fixed from the beginning and types are managed by
  41. David> the compiler. If the runtime had anything to do with the type system in
  42. David> C++, maybe there would be such a thing as a virtual constructor.
  43.  
  44. I don' think the question was why to call a virtual constructor for a
  45. base class, but why to call a constructor for a virtual base class.
  46.  
  47. These constructors need to be called to ensure that the data members
  48. of any parent classes are properly initialized. For instance:
  49.  
  50. class VirtBase
  51. {
  52.   private:
  53.     int     a;
  54.  
  55.   protected:
  56.     int     myA( ) const            { return a; }
  57.     int     myA( const int anA )    { return a = anA; }
  58.  
  59.   public:
  60.     VirtBase( const int anA ) : a( anA ) { }
  61. };
  62.  
  63. class DerivedClass : virtual public VirtBase
  64. {
  65.   public:
  66.     DerivedClass( const int anA ) : VirtBase( anA ) { }
  67. };
  68.  
  69. If VirtBase weren't initialized in the constructor for DerivedClass,
  70. VirtBase::a would never be initialized. This is one reason why you
  71. should always explicitly call the constructor for any base classes in
  72. a derived classes constructor.
  73.  
  74. bab
  75. --
  76. |-----------------------|----------------------------------------------------|
  77. | Brian Button          | email : button@wg2.waii.com                        |
  78. | Design Engineer       |         71023.276@compuserve.com                   |
  79. | Western Geophysical   | voice : (713)964-6221                              |
  80. | 3600 Briarpark        |----------------------------------------------------|
  81. | Houston, Texas  77042 | OS/2 - Used at only the BEST Pool Halls ;)         |
  82. |-----------------------|----------------------------------------------------|
  83.