home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15999 < prev    next >
Encoding:
Text File  |  1992-11-09  |  3.0 KB  |  81 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Calling pure virtual functions in base class constructor
  5. Message-ID: <1992Nov10.010626.26528@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <720990361snx@trmphrst.demon.co.uk>
  10. Date: Tue, 10 Nov 1992 01:06:26 GMT
  11. Lines: 68
  12.  
  13. In article <720990361snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk writes:
  14. >In article <miLRVB10w165w@csource.oz.au> david@csource.oz.au writes:
  15. >> I'm somewhat puzzled as to why the current rule on calling pure virtual
  16. >> functions from a base class does not simply prohibit them from being
  17. >> there (ie. by a compiler error) rather than defining the results as
  18. >> being 'undefined'. (ARM sec 12.7)
  19. >The reason is that it is quite legal to have a definition of a pure 
  20. >virtual function, thus ...
  21. >
  22. >class A
  23. >  {
  24. >    // ...
  25. >      ~A() { purefunc(); }
  26. >      virtual void purefunc() = 0;
  27. >  };
  28. >
  29. >void A::purefunc()
  30. >{
  31. >  cout << "You called a pure virtual function !\n";
  32. >}
  33. >
  34. >This facility might be useful when you want your abstract base class to 
  35. >provide some kind of default behaviour, but you want to force derived 
  36. >classes to add additional behaviour (or, at least, hint strongly :-).
  37.  
  38.     Not quite. It does not supply 'default behaviour', but 
  39. is called in two by a statically, i.e. like
  40.  
  41.     A::purefunc(); // from inside a member
  42.     a.purefunc(); // from outside the object
  43.  
  44. >
  45. >Actually, it occurrs to me that it should be possible to do this yourself,
  46. >without adding to code size (probably) - just declare your virtual 
  47. >functions ...
  48. >
  49. >class A
  50. >  {
  51. >    // ...
  52. >      ~A() { purefunc(); }
  53. >      virtual void purefunc() = 0;
  54. >  };
  55. >
  56. >inline void A::purefunc() { assert(0); }
  57.  
  58.     No, this cannot work. The body of a pure virtual cannot be
  59. called though a pointer or reference, only from a 'value'. Had
  60. the pure specifier not been included, the body would have been
  61. called virtually during construction of the A base only.
  62. >
  63. >Pure virtual functions can only be called explicitly, under circumstances 
  64. >where the actual type can be deduced at compile time, which are the 
  65. >conditions allowing inline virtuals to be expanded inline. Then again, I 
  66. >could be wrong.
  67.  
  68.     IMHO you are wrong. It is not a matter of deduction, the programmer
  69. must explicitly override the virtual call mechanism.  Virtual calls
  70. (indirections via the vtble) can be replaced by direct calls only
  71. in code outside the object where the compiler can deduce the actual
  72. object type, this cannot ever be the case inside a member function
  73. as far as I can see (because no member can know what the most derived
  74. object is).
  75.  
  76. -- 
  77. ;----------------------------------------------------------------------
  78.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  79.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  80. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  81.