home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16190 < prev    next >
Encoding:
Internet Message Format  |  1992-11-13  |  5.3 KB

  1. Path: sparky!uunet!know!cass.ma02.bull.com!mips2!news.bbn.com!usc!zaphod.mps.ohio-state.edu!wupost!darwin.sura.net!convex!news.utdallas.edu!corpgate!bnrgate!bnr.co.uk!pipex!demon!trmphrst.demon.co.uk!nikki
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Calling pure virtual functions in base class constructor
  5. Message-ID: <721505557snx@trmphrst.demon.co.uk>
  6. Date: 11 Nov 92 11:12:37 GMT
  7. Sender: usenet@gate.demon.co.uk
  8. Reply-To: nikki@trmphrst.demon.co.uk
  9. Organization: Trumphurst Ltd.
  10. Lines: 115
  11. X-Mailer: cppnews $Revision: 1.20 $
  12.  
  13. In article <1992Nov10.010626.26528@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  14. > In article <720990361snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk writes:
  15. > > ... it is quite legal to have a definition of a pure 
  16. > >virtual function, thus ...
  17. > >
  18. > >class A
  19. > >  {
  20. > >    // ...
  21. > >      ~A() { purefunc(); }
  22. > >      virtual void purefunc() = 0;
  23. > >  };
  24. > >
  25. > >void A::purefunc()
  26. > >{
  27. > >  cout << "You called a pure virtual function !\n";
  28. > >}
  29. > >
  30. > >This facility might be useful when you want your abstract base class to 
  31. > >provide some kind of default behaviour, but you want to force derived 
  32. > >classes to add additional behaviour (or, at least, hint strongly :-).
  33. It seems I did not explain this too well. Ask yourself what use is a 
  34. definition of a pure virtual function ? It can only be called statically, 
  35. so why not make a non-virtual function (with a different name) to do the 
  36. job ? The only reason _I_ can think of is to hint to the person deriving 
  37. from the class that some of the common/default things they want to do in 
  38. their derived function are already written for them. It is quite common in
  39. a virtual function in a derived class to call the base class version as
  40. well as adding derived class specific behaviour. This is why I used 
  41. the term "default behaviour".
  42.  
  43. Alternative explanations for the _usefulness_ of definitions of pure 
  44. virtuals welcome.
  45.  
  46. >     Not quite. It does not supply 'default behaviour', but 
  47. > is called in two by a statically, i.e. like
  48.   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sorry, don't understand a word of this !
  49. >     A::purefunc(); // from inside a member
  50. Yes, this is how you force a call to a pure virtual. 
  51. >     a.purefunc(); // from outside the object
  52. Surely this will NEVER call the pure virtual, because you cannot have an
  53. object of type A (A is an abstract class).
  54.  
  55. We were discussing a call in a base class destructor. I thought this would
  56. always call the pure virtual (because any derived class object has
  57. already been destructed at that point). However, I have re-read the ARM 
  58. (s10.3 p215), and it gives an example ...
  59.  
  60. arm> class A {
  61. arm>     virtual void f() = 0;
  62. arm>     A() {
  63. arm>         A::f();    // ok
  64. I ask, why is the A:: necessary here ? If f was NOT pure, A::f would 
  65. definitely be called, because there is no other f is available during the 
  66. constructor. Is it just to make the compiler writer's life one tiny bit 
  67. easier (i.e. always despatch via the vtable unless A:: is present) ?
  68. arm>     }
  69. arm> };
  70. arm> 
  71. arm> void A::f()        // defined somewhere
  72. arm> {
  73. arm>     // ...
  74. arm> }
  75.  
  76. The arm also states (s12.7 p295)
  77. arm> The effect of calling a pure virtual function directly or indirectly
  78. arm> for the object being constructed from a constructor, except using
  79. arm> explicit qualification, is undefined.
  80. So the insistence on the A:: qualification is definitely deliberate. Can 
  81. anyone explain why ? (Bjarne ?)
  82.  
  83. > >
  84. > >Actually, it occurrs to me that it should be possible to do this yourself,
  85. > >without adding to code size (probably) - just declare your virtual 
  86. > >functions ...
  87. > >
  88. > >class A
  89. > >  {
  90. > >    // ...
  91. > >      ~A() { purefunc(); }
  92. > >      virtual void purefunc() = 0;
  93. > >  };
  94. > >
  95. > >inline void A::purefunc() { assert(0); }
  96. >     No, this cannot work. The body of a pure virtual cannot be
  97. > called though a pointer or reference, only from a 'value'. Had
  98. > the pure specifier not been included, the body would have been
  99. > called virtually during construction of the A base only.
  100.  You CAN (IMHO) call a pure virtual using a pointer or reference, using
  101. pointer->A::purefunc();
  102.  
  103. > >Pure virtual functions can only be called explicitly, under circumstances 
  104. > >where the actual type can be deduced at compile time, which are the 
  105. > >conditions allowing inline virtuals to be expanded inline. Then again, I 
  106. > >could be wrong.
  107. >     IMHO you are wrong. It is not a matter of deduction, the programmer
  108. > must explicitly override the virtual call mechanism.  Virtual calls
  109. > (indirections via the vtble) can be replaced by direct calls only
  110. > in code outside the object where the compiler can deduce the actual
  111. > object type, this cannot ever be the case inside a member function
  112. > as far as I can see (because no member can know what the most derived
  113. > object is).
  114. After my re-reading of the ARM, I realise that I am, indeed, wrong. 
  115. Despite the fact that the compiler can easily convert calls to any
  116. virtual function (pure or otherwise) to a direct call in a constructor or 
  117. destructor, the ARM states that it should NOT do so in the case of pure 
  118. virtuals.
  119.  
  120. Sorry about the length of this posting, but I really would like to 
  121. understand the reasons for all this :-)
  122. -- 
  123. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  124. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  125.