home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11375 < prev    next >
Encoding:
Text File  |  1992-07-22  |  2.6 KB  |  75 lines

  1. Path: sparky!uunet!mcsun!fuug!demos!kiae!glas!demos!early-bird.think.co!think.com!barmar
  2. From: barmar@think.com
  3. Newsgroups: comp.lang.c++
  4. Date: 17 Jul 92 19:26 MDT
  5. Subject: Re: virtual call efficiency (Re: ar
  6. Sender: Notesfile to Usenet Gateway <notes@glas.apc.org>
  7. Message-ID: <146oruINNiom@early-bird.think.co>
  8. References: <9219919.15720@mulga.cs.mu.oz.au>
  9. Nf-ID: #R:9219919.15720@mulga.cs.mu.oz.au:-1880726321:146oruINNiom@early-bird.think.co:121153198:001:2138
  10. Nf-From: think.com!barmar    Jul 17 19:26:00 1992
  11. Lines: 62
  12.  
  13.  
  14. In article <1992Jul17.114522.15858@tfs.com> eric@tfs.com (Eric Smith) writes:
  15. >There is one case where the virtual function would appear to be doing nothing
  16. >but simple assignment, and that is where each concrete subclass may or may
  17. >not implement it as appropriate for that subclass, and the base class defaults
  18. >the action if not implemented in the subclass.  In that case, you have the
  19. >superficial appearance of a function that does nothing but assignment, but it's
  20. >really accomplishing more, because if you inlined it you would have to inline
  21. >a switch to test whether to use the concrete assignment or default, and that
  22. >would add a lot more overhead than the virtual function call.
  23.  
  24. This is probably a case where benchmarking would be appropriate.  It's
  25. quite possible that the overhead of such a test would be less than the
  26. overhead of a virtual call.
  27.  
  28. At first I thought that this wouldn't be true, since you'd probably want to
  29. use a virtual call to find out whether the access should be done directly
  30. or via a call, and that would clearly be worse than just a virtual call to
  31. perform the access.  But then I realized that the base class could contain
  32. a data member that the derived class could fill in.
  33.  
  34. class Base {
  35.     private:
  36.     int foo;
  37.     protected:
  38.     int foo_direct;
  39.         virtual void internal_set_foo (int newfoo): { foo = newfoo; };
  40.     virtual int internal_get_foo () { return foo; };
  41.     public:
  42.         Base(int initfoo = 0): foo(initfoo), foo_direct(1) {};
  43.     void set_foo (int newfoo)
  44.         { if (foo_direct) foo = newfoo; else internal_set_foo(newfoo); };
  45.     void get_foo ()
  46.         { return foo_direct ? foo : internal_get_foo();p };
  47. };
  48.  
  49. class Derived: public Base {
  50.     protected:
  51.     void internal_set_foo (int newfoo);
  52.     int internal_get_foo ();
  53.     public:
  54.     Derived(int initfoo = 0);
  55. }
  56.  
  57. Derived::Derived(int initfoo = 0) {
  58.     foo_direct = 0;
  59.     /* Code to reinitialize foo from initfoo */
  60. }
  61.  
  62. void Derived::internal_set_foo(int newfoo) {
  63.     /* ... */
  64. }
  65.  
  66. int Derived::internal_get_foo() {
  67.     /* ... */
  68. }
  69. -- 
  70. Barry Margolin
  71. System Manager, Thinking Machines Corp.
  72.  
  73. barmar@think.com          {uunet,harvard}!think!barmar
  74.  
  75.