Do functions defined inside a class need inline keyword ?

Discussion in 'C++' started by ganesh.gella@gmail.com, Jul 13, 2005.

  1. Guest

    Hi All,

    Wanted to make sure on this,

    I have some functions for which I am providing the definition inside
    the class definition. (As these are very small functions like get and
    set).

    Do i need to declare these functions as inline (I would like them to be
    inlined) ? or are these functions considered for inline by default as
    the definition is provided as part of the class ?

    -Thanks
    Ganesh
    , Jul 13, 2005
    #1
    1. Advertising

  2. Stephen Howe Guest

    > I have some functions for which I am providing the definition inside
    > the class definition. (As these are very small functions like get and
    > set).
    >
    > Do i need to declare these functions as inline (I would like them to be
    > inlined) ?


    No.

    >... or are these functions considered for inline by default as
    > the definition is provided as part of the class ?


    Yes. You got it right.

    Stephen Howe
    Stephen Howe, Jul 13, 2005
    #2
    1. Advertising

  3. wrote:
    > Hi All,
    >
    > Wanted to make sure on this,
    >
    > I have some functions for which I am providing the definition inside
    > the class definition. (As these are very small functions like get and
    > set).
    >
    > Do i need to declare these functions as inline (I would like them to be
    > inlined) ? or are these functions considered for inline by default as
    > the definition is provided as part of the class ?


    As far as I remember a function defined inside your class definition
    will be considered as an inline function.

    class A {
    public:
    int getSomething() { [method definition] };
    }

    would be the same as :

    class A {
    public:
    int getSomething();
    }

    inline int A::getSomething() {
    [method definition]
    }

    Correct me if im wrong.
    Einar Forselv, Jul 13, 2005
    #3
  4. Jaspreet Guest

    wrote:
    > Hi All,
    >
    > Wanted to make sure on this,
    >
    > I have some functions for which I am providing the definition inside
    > the class definition. (As these are very small functions like get and
    > set).
    >
    > Do i need to declare these functions as inline (I would like them to be
    > inlined) ?


    No need to declare them as inline because functions defined inside a
    class are considered inline if compiler can make them inline. It should
    work in your case since you are using simple get and set functions.

    or are these functions considered for inline by default as
    > the definition is provided as part of the class ?


    You are correct.

    >
    > -Thanks
    > Ganesh
    Jaspreet, Jul 13, 2005
    #4
  5. upashu2 Guest

    >> need to declare them as inline because functions defined inside a
    >>class are considered inline if compiler can make them inline.

    The compiler treats the inline expansion options and keywords as
    suggestions. There is no guarantee that functions will be inlined. You
    cannot force the compiler to inline a particular function.

    A class's member functions can be declared inline either by using the
    inline keyword or by placing the function definition within the class
    definition.

    >> are these functions considered for inline by default as
    > >the definition is provided as part of the class ?

    Yes.
    upashu2, Jul 13, 2005
    #5
  6. Jaspreet Guest

    upashu2 wrote:
    > >> need to declare them as inline because functions defined inside a
    > >>class are considered inline if compiler can make them inline.

    > The compiler treats the inline expansion options and keywords as
    > suggestions. There is no guarantee that functions will be inlined. You
    > cannot force the compiler to inline a particular function.
    >
    > A class's member functions can be declared inline either by using the
    > inline keyword or by placing the function definition within the class
    > definition.


    Somehow the "No" word and the rest of the post went astray and there
    was a gap of 2 lines in between which changed the whole meaning of my
    post :(

    Apologies for that. Please read my post with its first word as "No".

    :(
    Jaspreet, Jul 14, 2005
    #6
    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. Replies:
    3
    Views:
    457
  2. Daniel Vallstrom
    Replies:
    2
    Views:
    1,876
    Kevin Bracey
    Nov 21, 2003
  3. Nish
    Replies:
    4
    Views:
    487
    Thomas Stegen
    Oct 8, 2004
  4. Oodini
    Replies:
    1
    Views:
    1,767
    Keith Thompson
    Sep 27, 2005
  5. Rahul
    Replies:
    3
    Views:
    454
    James Kanze
    Feb 28, 2008
Loading...

Share This Page