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

  1. Xref: sparky comp.lang.c++:15910 comp.std.c++:1508
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  4. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  5. Subject: Re: Calling pure virtual functions in base class constructor
  6. Reply-To: nikki@trmphrst.demon.co.uk
  7. Distribution: world
  8. Followup-To: comp.lang.c++
  9. X-Mailer: cppnews $Revision: 1.20 $
  10. Organization: Trumphurst Ltd.
  11. Lines: 56
  12. Date: Thu, 5 Nov 1992 12:06:01 +0000
  13. Message-ID: <720990361snx@trmphrst.demon.co.uk>
  14. Sender: usenet@gate.demon.co.uk
  15.  
  16. In article <miLRVB10w165w@csource.oz.au> david@csource.oz.au writes:
  17. > I'm somewhat puzzled as to why the current rule on calling pure virtual
  18. > functions from a base class does not simply prohibit them from being
  19. > there (ie. by a compiler error) rather than defining the results as
  20. > being 'undefined'. (ARM sec 12.7)
  21. The reason is that it is quite legal to have a definition of a pure 
  22. virtual function, thus ...
  23.  
  24. class A
  25.   {
  26.     // ...
  27.       ~A() { purefunc(); }
  28.       virtual void purefunc() = 0;
  29.   };
  30.  
  31. void A::purefunc()
  32. {
  33.   cout << "You called a pure virtual function !\n";
  34. }
  35.  
  36. This facility might be useful when you want your abstract base class to 
  37. provide some kind of default behaviour, but you want to force derived 
  38. classes to add additional behaviour (or, at least, hint strongly :-).
  39.  
  40. > What is interesting to note is how the various compilers handled it.
  41. > MSC/C++ 7 calls a null function via the vtable (cute), resulting in
  42. > a system crash or main() being re-entered depending on memory model,
  43. > but even more interesting is gcc 2.2.2 (djgpp), which generated a call
  44. > to abort() inline and threw out the original code completely! In other
  45. > words, it clearly knew that the code was wrong, but it silently did
  46. > this, even at -Wall.
  47. It looks like g++ is being clever, and making undefined pure virtual 
  48. function entries in the vtable point to a piece of code that does an 
  49. abort. 
  50.  
  51. Good idea - are you listening Microsoft ? [Answer : Not officially :-]
  52.  
  53. Actually, it occurrs to me that it should be possible to do this yourself,
  54. without adding to code size (probably) - just declare your virtual 
  55. functions ...
  56.  
  57. class A
  58.   {
  59.     // ...
  60.       ~A() { purefunc(); }
  61.       virtual void purefunc() = 0;
  62.   };
  63.  
  64. inline void A::purefunc() { assert(0); }
  65.  
  66. Pure virtual functions can only be called explicitly, under circumstances 
  67. where the actual type can be deduced at compile time, which are the 
  68. conditions allowing inline virtuals to be expanded inline. Then again, I 
  69. could be wrong.
  70.  
  71. [This doesn't make you a smeg-head :-]
  72. -- 
  73. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  74. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  75.