inline functions in a c++ class

Discussion in 'C++' started by Rahul, Feb 24, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I had this doubt for quite some while, does defining a member
    function within the class declaration makes it inline by default?
    Would the compiler attempt to inline all calls to such member
    functions?

    class sample
    {
    public: void print()
    {
    cout<<"sample::print function invoked";
    }
    };

    int main()
    {
    sample obj;
    obj.print();
    return(0);
    }

    Thanks in advance!!!
     
    Rahul, Feb 24, 2008
    #1
    1. Advertising

  2. Rahul

    Rolf Magnus Guest

    Rahul wrote:

    > Hi Everyone,
    >
    > I had this doubt for quite some while, does defining a member
    > function within the class declaration makes it inline by default?


    Yes, if you mean that it has the same efffect as using the inline keyword on
    the function.

    > Would the compiler attempt to inline all calls to such member
    > functions?


    It might do that, or not. The only guaranteed effect of the inline keyword
    in C++ is that there can be multiple definitions of the function in
    different translation units, even if that function has external linkage.
    Regarding optimization, the inline keyword is just a hint to the compilier
    that some compilers simply ignore, deciding on their own when to inline a
    function call and when not to.
     
    Rolf Magnus, Feb 24, 2008
    #2
    1. Advertising

  3. Rahul

    Rahul Guest

    >
    > It might do that, or not. The only guaranteed effect of the inline keyword
    > in C++ is that there can be multiple definitions of the function in
    > different translation units, even if that function has external linkage.


    I didn't know that... do you have any links for further info on this
    one?
     
    Rahul, Feb 24, 2008
    #3
  4. Rahul

    Ian Collins Guest

    Rahul wrote:
    >> It might do that, or not. The only guaranteed effect of the inline keyword
    >> in C++ is that there can be multiple definitions of the function in
    >> different translation units, even if that function has external linkage.

    >
    > I didn't know that... do you have any links for further info on this
    > one?


    The standard?

    --
    Ian Collins.
     
    Ian Collins, Feb 25, 2008
    #4
  5. Rahul wrote:
    > I had this doubt for quite some while, does defining a member
    > function within the class declaration makes it inline by default?


    Yes, it does.

    > Would the compiler attempt to inline all calls to such member
    > functions?


    It depends on many factors and in reality virtually all of them are
    implementation-dependent. It might inline all calls, some of the calls,
    or none of the calls. The decision to inline can really be made on
    per-call basis.

    Moreover, the compiler might inline calls to the functions that are not
    explicitly declared 'inline' at its own discretion. For this reason, the
    relationship between the 'inline' specifier and the actual inlining of
    the calls might be (and is) significantly more loose then it is often
    assumed to be. As other already noted, the only thing the 'inline'
    specifier is guaranteed to do is to allow multiple definitions of the
    same function with external linkage in the program.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Feb 25, 2008
    #5
    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:
    741
    E. Robert Tisdale
    Jul 3, 2003
  2. Replies:
    3
    Views:
    460
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    1,895
    Kevin Bracey
    Nov 21, 2003
  4. jamihuq

    converting inline functions to C functions

    jamihuq, May 16, 2006, in forum: C Programming
    Replies:
    7
    Views:
    345
  5. Rahul
    Replies:
    3
    Views:
    457
    James Kanze
    Feb 28, 2008
Loading...

Share This Page