c++ - Are inline virtual functions really a non-sense? -


i got question when received code review comment saying virtual functions need not inline.

i thought inline virtual functions come in handy in scenarios functions called on objects directly. counter-argument came mind -- why 1 want define virtual , use objects call methods?

is best not use inline virtual functions, since they're never expanded anyway?

code snippet used analysis:

class temp { public:      virtual ~temp()     {     }     virtual void myvirtualfunction() const     {         cout<<"temp::myvirtualfunction"<<endl;     }  };  class tempderived : public temp { public:      void myvirtualfunction() const     {         cout<<"tempderived::myvirtualfunction"<<endl;     }  };  int main(void)  {     tempderived aderivedobj;     //compiler thinks it's safe expand virtual functions     aderivedobj.myvirtualfunction();      //type of object temp points known;     //does compiler still expand virtual functions?     //i doubt compiler intelligent!     temp* ptemp = &aderivedobj;     ptemp->myvirtualfunction();      return 0; } 

virtual functions can inlined sometimes. excerpt excellent c++ faq:

"the time inline virtual call can inlined when compiler knows "exact class" of object target of virtual function call. can happen when compiler has actual object rather pointer or reference object. i.e., either local object, global/static object, or contained object inside composite."


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -