problem on inline virtual function.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shuisheng

    #1

    problem on inline virtual function.

    Dear All,

    If I define a virtual function to be inline, is it really inline? Or it
    is inline in some cases, and not in other cases. Would you please help
    me to look at the following case.

    struct A
    {
    int i;
    A() {i = 0;}
    virtual void Add() {i++;}
    };

    struct B : public A
    {
    virtual void Add() {i += 2;}
    };

    int main()
    {
    A a;
    a.Add(); // inline ?

    B b;
    b.Add(); // inline ?

    A *pa = new A();
    pa->Add(); // not inline?

    A *pb = new B();
    pb->Add(); // not inline?

    return 0;
    }

    Thanks!

    Shuisheng

  • Gianni Mariani

    #2
    Re: problem on inline virtual function.

    shuisheng wrote:
    Dear All,
    >
    If I define a virtual function to be inline, is it really inline? Or it
    is inline in some cases, and not in other cases. Would you please help
    me to look at the following case.
    It depends on the compiler, however it is nigh impossible for a compiler
    to inline (as in inline the generated code) a virtual funtion.

    Comment

    • Victor Bazarov

      #3
      Re: problem on inline virtual function.

      shuisheng wrote:
      If I define a virtual function to be inline, is it really inline?
      No function you *declare* 'inline' is really inline. There is no way
      in the language to find out. The compiler will do what it wants.
      Or
      it is inline in some cases, and not in other cases. Would you please
      help me to look at the following case.
      >
      struct A
      {
      int i;
      A() {i = 0;}
      virtual void Add() {i++;}
      };
      >
      struct B : public A
      {
      virtual void Add() {i += 2;}
      };
      >
      int main()
      {
      A a;
      a.Add(); // inline ?
      Yes, very likely.
      >
      B b;
      b.Add(); // inline ?
      Yes, very likely.
      >
      A *pa = new A();
      pa->Add(); // not inline?
      >
      A *pb = new B();
      pb->Add(); // not inline?
      >
      return 0;
      }
      The last two cases, I don't know, but I suspect that only the
      last time the call will be non-inlined (polymorphic). Both
      cases are possible to track since 'pa' and 'pb' are local objects,
      and the compiler _may_ be written to track those things, but no
      guarantees can be given.

      Ask your compiler to generate assembly code for you and look.

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • Kevin Handy

        #4
        Re: problem on inline virtual function.

        shuisheng wrote:
        Dear All,
        >
        If I define a virtual function to be inline, is it really inline? Or it
        is inline in some cases, and not in other cases. Would you please help
        me to look at the following case.
        >
        struct A
        {
        int i;
        A() {i = 0;}
        virtual void Add() {i++;}
        };
        >
        struct B : public A
        {
        virtual void Add() {i += 2;}
        };
        >
        int main()
        {
        A a;
        a.Add(); // inline ?
        >
        B b;
        b.Add(); // inline ?
        >
        A *pa = new A();
        pa->Add(); // not inline?
        >
        A *pb = new B();
        pb->Add(); // not inline?
        >
        return 0;
        }
        >
        Thanks!
        >
        Shuisheng
        >
        I t d e p e n d s o n y o u r c o m p i l e r .
        " i n l i n e " i s j u s t a h i n t ,
        i t i s n o t a l a w .

        ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

        Comment

        Working...