virtual inline

Discussion in 'C++' started by .rhavin grobert, Nov 14, 2008.

  1. if you have

    class Base {
    virtual inline bool foo() {return false;};
    };

    class Derived: public Base {
    virtual bool foo();
    };


    bool Derived:foo()
    {
    /* lot's of code here */
    return true;
    };

    is foo() always inlined for Base? is foo always (jumped /
    called / ...) (<-?) for Derived?
     
    .rhavin grobert, Nov 14, 2008
    #1
    1. Advertising

  2. Hi!

    ..rhavin grobert schrieb:
    > class Base {
    > virtual inline bool foo() {return false;};
    > };
    >
    > class Derived: public Base {
    > virtual bool foo();
    > };
    >
    > bool Derived:foo()
    > {
    > /* lot's of code here */
    > return true;
    > };
    >
    > is foo() always inlined for Base? is foo always (jumped /
    > called / ...) (<-?) for Derived?


    From your code Base::foo is never called at all, so you cannot answer
    the question whether it is expanded inline or not.

    Derived::foo cannot be expanded inline unless your compiler uses a two
    pass method to examine the body of the function.

    Furthermore, a virtual function may not be expanded inline unless the
    compiler knows for sure, that it cannot be overloaded. The reason is
    simply that the required function is determined at runtime and may not
    even be written at the time when the compiler generates the call.
    Only if the type is known for sure, the run time dispatch can be
    optimized. E.g.
    Base b;
    b.foo();
    may expand Base::foo inline.
    But as soon as b becomes a reference or pointer type, a run time
    dispatch is required.
    (Languages with a 'final' attribute for functions and classes have a
    significantly higher probability of the above optimization.)


    Marcel
     
    Marcel Müller, Nov 15, 2008
    #2
    1. Advertising

  3. .rhavin grobert

    Guest

    On Nov 14, 10:50 pm, ".rhavin grobert" <> wrote:
    > if you have
    >
    > class Base {
    > virtual inline bool foo() {return false;};
    >
    > };
    >
    > class Derived: public Base {
    > virtual bool foo();
    >
    > };
    >
    > bool Derived:foo()
    > {
    > /* lot's of code here */
    > return true;
    >
    > };
    >
    > is foo() always inlined for Base? is foo always (jumped /
    > called / ...) (<-?) for Derived?


    For more information visit this link:
    http://groups.google.com/group/CPP-...inlining-virtual-functions-and-inlining?hl=en

    --
    Daya
     
    , Nov 17, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    766
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    511
    E. Robert Tisdale
    May 6, 2005
  3. Replies:
    3
    Views:
    494
  4. Daniel Vallstrom
    Replies:
    2
    Views:
    2,058
    Kevin Bracey
    Nov 21, 2003
  5. Nish
    Replies:
    4
    Views:
    547
    Thomas Stegen
    Oct 8, 2004
Loading...

Share This Page