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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Is this C++ advice from an OO book true? (beginner question)
  5. Message-ID: <1993Jan24.163907.18298@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <sherman.727849469@foster>
  8. Date: Sun, 24 Jan 1993 16:39:07 GMT
  9. Lines: 50
  10.  
  11. sherman@unx.sas.com (Chris Sherman) writes:
  12.  
  13. >  11.  Call virtual class constructors in dervied class constructors.
  14.  
  15. >     I pity the poor fools who have to spend nights and weekends searching
  16. >     for the reason that a class variable occasionally takes on strange,
  17. >     unexpected values .  .  .  only to find through their bleary eyes
  18. >     that they forgot to allocate space for a virtual base (super) class,
  19. >     so that storage is allocated for the base class's members.  Even if
  20. >     the class has no members, declare it--you never know what future
  21. >     changes will come back to haunt you (or the person who inherits your
  22. >     mine field)!
  23.  
  24. Let's assume the wording is poor and the writer means "call the constructor
  25. for virtual base classes in derived class constructors".  This is not
  26. necessarily bad advice, but the reason given is nonsensical.
  27.  
  28. The constructors for all virtual base classes in a hierarchy are called
  29. when beginning construction of the entire (most-derived) object.  If
  30. the most-derived constructor does not specify which constructor to call
  31. for a virtual base class, the default constructor will be called.  This
  32. is the case even if an intermediate class appears to invoke a particular
  33. base class constructor.  Simple example:
  34.  
  35.     class Base { ... Base(); Base(int); };
  36.     class D1 : public virtual Base {
  37.         ...
  38.         D1(int i) : Base(i) { ... };
  39.     };
  40.     class D2 : public D1 {
  41.         ...
  42.         D2(int i) : D1(i) { ... }
  43.     };
  44.     D2 d(2);
  45.  
  46. When constructing object d, there is no explicit invocation of a Base
  47. constructor in D2::D2(int), so Base::Base() is invoked.  This might seem
  48. surprising if you don't know the rule.  If Base did not have a default
  49. constructor, the example would not even compile.
  50.  
  51. You can call out explicitly the virtual base class constructors if you wish:
  52.     D2(int i) : Base(i), D1(i) { ... }
  53. This is necessary if you want a particular constructor for a virtual base
  54. class, since you will otherwise get the default initializer (or an error).
  55.  
  56. On the other hand, if you intend for the default constructor to be called,
  57. you need do nothing, since that is what you will get.
  58. -- 
  59.  
  60. Steve Clamage, TauMetric Corp, steve@taumet.com
  61.