Function declaration in class declaration

Discussion in 'C++' started by Ovidesvideo, Dec 10, 2004.

  1. Ovidesvideo

    Ovidesvideo Guest

    Just a quick question. If I declare functions in the class declaration as such:

    class myclass
    {
    public:
    void function(int &x)
    {
    x++;
    }
    };

    Will the function be called normally, or simply inlined in the code?
    Ovidesvideo, Dec 10, 2004
    #1
    1. Advertising

  2. Ovidesvideo wrote in news:
    in comp.lang.c++:

    > Just a quick question. If I declare functions in the class declaration
    > as such:
    >
    > class myclass
    > {
    > public:
    > void function(int &x)
    > {
    > x++;
    > }
    > };
    >
    > Will the function be called normally, or simply inlined in the code?
    >


    That is implementation defined (i.e. its upto your compiler), but this
    rule applies to *all* inline functions. Many compilers have options that
    let you control how they handle this situation so read the manual.

    When compiling debuggable code some compilers won't inline anything.

    Some compilers (when optimizing) will inline functions that wern't
    defined as inline.

    But for normal (non-debug) code you can reasonable expect that small
    functions like your example will be inlined, you just can't rely on it.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Dec 10, 2004
    #2
    1. Advertising

  3. Ovidesvideo

    gianguz Guest

    The function will be inline expanded.
    This is not an implementation defined behaviour but it is part
    of the semantic of inline declarations.

    Look at the following:

    class A {
    public
    A() {}
    inline void a();
    void b() {}
    };

    A::a() {}

    a() and b() are semantically equivalent.

    Gianguglielmo
    gianguz, Dec 10, 2004
    #3
  4. gianguz wrote in news:
    in comp.lang.c++:

    > The function will be inline expanded.
    >


    The Standard says this is *prefered* not *required*.

    See 7.1.2/2

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Dec 10, 2004
    #4
  5. Ovidesvideo wrote:

    > Just a quick question. If I declare functions in the class declaration as such:
    >
    > class myclass
    > {
    > public:
    > void function(int &x)
    > {
    > x++;
    > }
    > };
    >
    > Will the function be called normally, or simply inlined in the code?


    A function declared in this fashion is an 'inline' function. Whether the
    calls to this function will actually be inlined depends on the
    compiler's decision. Moreover, this decision can be made independently
    for each call to this function - some calls might get inlined and some not.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Dec 10, 2004
    #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. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  2. kelvSYC
    Replies:
    6
    Views:
    7,215
    Richard Herring
    May 17, 2005
  3. Replies:
    4
    Views:
    1,053
    Richard Tobin
    Dec 12, 2006
  4. Bolin
    Replies:
    4
    Views:
    402
  5. Luca Forlizzi
    Replies:
    4
    Views:
    448
    Luca Forlizzi
    Nov 14, 2010
Loading...

Share This Page