Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. virtual function

virtual function

Scheduled Pinned Locked Moved C / C++ / MFC
designquestioncode-review
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    richardye
    wrote on last edited by
    #1

    Inside <>, it wrote "It is stll a bad design choice to declare all functions virutal and to depend on the compiler to optimize away unnecessary virtual invocations." I can't understand it very well. class Abstract_base { public: virtual ~Abstract_base()=0; virtual void interface1() const = 0; virtual const char* mumble() const {return _mumble;) protected: char *_mumble; }; As the source above. "virtual const char* mumble() const {return _mumble;)" If you define a member function to be virtual, you insert a vptr in your class object. but as the function's defination has been claimed within the head file, it must be looked as an linline function. Is it right? Tomorrow is another day!

    M 1 Reply Last reply
    0
    • R richardye

      Inside <>, it wrote "It is stll a bad design choice to declare all functions virutal and to depend on the compiler to optimize away unnecessary virtual invocations." I can't understand it very well. class Abstract_base { public: virtual ~Abstract_base()=0; virtual void interface1() const = 0; virtual const char* mumble() const {return _mumble;) protected: char *_mumble; }; As the source above. "virtual const char* mumble() const {return _mumble;)" If you define a member function to be virtual, you insert a vptr in your class object. but as the function's defination has been claimed within the head file, it must be looked as an linline function. Is it right? Tomorrow is another day!

      M Offline
      M Offline
      Mattias G
      wrote on last edited by
      #2

      I think what the author meant was that the compiler does not generate a vtable entry that is never needed. If you declare a function as virtual in a base class, and that function is always called in the scope of a known class, the compiler might as well skip the vtable entry for that function. Or if you declare a virtual fuction in a class with no sub- or superclasses, there's no point in using a vtable at all. As for the inline declaration of a function, it only tells the compiler that you would prefer to have the code generated inline. If the function is virtual, you will still get a pointer in the vtable to that function (unless optimized away by the compiler). Might add that I'm not an expert on the inner logic of an actual compiler, so the case may be a bit more complicated than this :-) -- modified at 12:33 Thursday 25th October, 2007

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups