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. how compiler differentiates inline virtual function? [modified]

how compiler differentiates inline virtual function? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 4 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.
  • Y Offline
    Y Offline
    yogish293
    wrote on last edited by
    #1

    Hi, suppose if i declare any virtual function as an inline how it differentiates from compile time ? Base class object pointer variable may reference to inherited object.How inline works in such case? thank you

    modified on Thursday, August 25, 2011 9:24 AM

    C S 2 Replies Last reply
    0
    • Y yogish293

      Hi, suppose if i declare any virtual function as an inline how it differentiates from compile time ? Base class object pointer variable may reference to inherited object.How inline works in such case? thank you

      modified on Thursday, August 25, 2011 9:24 AM

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Since it can't it won't inline it (inline is just a tip for the compiler). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      N 1 Reply Last reply
      0
      • Y yogish293

        Hi, suppose if i declare any virtual function as an inline how it differentiates from compile time ? Base class object pointer variable may reference to inherited object.How inline works in such case? thank you

        modified on Thursday, August 25, 2011 9:24 AM

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        You can inline virtual functions just like any others: either by using the 'inline' keyword, or just by writing the function definition right into the declaration. Sometimes this makes sense (such as for destructors), sometimes it doesn't. It mostly depends on whether or not a compiler is able to decide what class a particular object pointer actually points to. Somtimes this can only be detemined at runtime and in that case, obviously, no inlining will occur. More information and examples can be found on the web, e. g. here: http://msdn.microsoft.com/en-us/magazine/cc301407.aspx[^] or here: http://drdobbs.com/cpp/184403747[^] In short - if the type of an object pointer can only be determined at runtime, then inlining is not possible, even if the base class and all derived classes do have inlined implementations of that function. If you really, absolutely, desperately need a function to be inlined, either make sure that a particular version is being called (if that is even possible), or simply don't use inheritance for that. That said, since inlining is really only the very last step in performance optimization, it's usally not a real issue: if you can't inline, then you just don't.

        1 Reply Last reply
        0
        • C CPallini

          Since it can't it won't inline it (inline is just a tip for the compiler). :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #4

          It is actually possible under certain conditions for the compiler to inline virtual functions. Consider the following:

          class Base
          {
          virtual void func() {...}
          };
          class Derived : public Base
          {
          virtual void func() {...}
          };

          Base b;
          b.func();
          Derived d;
          d.func();

          In both cases, the compiler can chose to inline func.

          C 1 Reply Last reply
          0
          • N Niklas L

            It is actually possible under certain conditions for the compiler to inline virtual functions. Consider the following:

            class Base
            {
            virtual void func() {...}
            };
            class Derived : public Base
            {
            virtual void func() {...}
            };

            Base b;
            b.func();
            Derived d;
            d.func();

            In both cases, the compiler can chose to inline func.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Yes, you are right, I should have written: "the compiler cannot always inline virtual functions...". However, the rationale behind virtual function is for supporting polymorphism, hence I assumed, in my answer, that late binding was the case of interest.

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            S 1 Reply Last reply
            0
            • C CPallini

              Yes, you are right, I should have written: "the compiler cannot always inline virtual functions...". However, the rationale behind virtual function is for supporting polymorphism, hence I assumed, in my answer, that late binding was the case of interest.

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              There are two rather common examples where virtual functions are resolved statically, and therefore can be inlined without problem: 1. Explicitely calling the base class implementation 2. Virtual destructors. The compiler will automatically create a chain of calls to destructors according to the inheritance chain. These are resolved statically. The only thing that may be resolved dynamically (at runtime) is the top level call, and even that only if the compiler cannot determine the actual class at compile time

              class Shape {
              bool state;
              public:
              inline virtual void initialize() {
              state = false;
              }
              inline virtual ~Shape() {} // will always be resolved statically!
              };

              class Square : public Shape {
              float width;
              public:
              virtual void initialize() {
              Shape::initialize(); // resolved statically - this can be inlined!
              width = 0.;
              }
              ~inline virtual ~Square() {} // may be resolved statically
              };

              int main() {
              Shape* pShape = new Square;
              pShape->initialize;
              delete pShape; // calls pShape->Shape::~Shape() and then pShape->Square::~Square()
              // this can be resolved statically since the compiler at this point knows the type
              return 0;
              }

              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