inline function in a class

Discussion in 'C++' started by kceiw, Sep 7, 2005.

  1. kceiw

    kceiw Guest

    How to make a member function inline while the declaration and
    implementation are separated?
     
    kceiw, Sep 7, 2005
    #1
    1. Advertising

  2. kceiw

    Squeamizh Guest

    kceiw wrote:
    > How to make a member function inline while the declaration and
    > implementation are separated?


    If you want to keep the declaration and body separate, then the easiest
    way to do it is by puttin the function body in the same header file as
    the class definition, but outside the actual class.

    E.G.:

    --- BEGIN EXAMPLE.H ----
    class Example {
    public:
    Example();
    inline void helloWorld() const;
    };

    void Example::helloWorld() const {
    std::cout << "hello world";
    }
    --- END EXAMPLE.H ---
     
    Squeamizh, Sep 7, 2005
    #2
    1. Advertising

  3. Squeamizh wrote:
    > kceiw wrote:
    > > How to make a member function inline while the declaration and
    > > implementation are separated?

    >
    > If you want to keep the declaration and body separate, then the easiest
    > way to do it is by puttin the function body in the same header file as
    > the class definition, but outside the actual class.
    >
    > E.G.:
    >
    > --- BEGIN EXAMPLE.H ----
    > class Example {
    > public:
    > Example();
    > inline void helloWorld() const;
    > };
    >
    > void Example::helloWorld() const {
    > std::cout << "hello world";
    > }
    > --- END EXAMPLE.H ---


    Your example wouldn't result in the member function being declared
    inline, as the OP specified. I think perhaps you meant something like:

    class Example {
    public:
    void memFunc() const;
    };

    inline void Example::memFunc() const
    {
    doSomething();
    }

    Best regards,

    Tom
     
    Thomas Tutone, Sep 7, 2005
    #3
  4. kceiw

    tmartsum Guest

    There is no way to completly force inline ....
    (what if your function is recursive ? =) )

    inline gives a hint (gcc will (normally?) respect this when given the
    right compilation options. (don't recall - RHFM).

    otherwise gcc will do the trick with __inline__.

    MS has __forceinline (I have only read about this never used it.)

    so you have to do something linke

    #ifdef WIN32
    // or maybe extent to win64 or make more macro ifs.
    #define STRONGER_INLINE_HINT __forceinline
    #else
    #define STRONGER_INLINE_HINT __inline__
    #endif

    Well maybe STRONGER_INLINE_HINT is a bit long ...

    /Thorbjørn
     
    tmartsum, Sep 7, 2005
    #4
  5. kceiw

    kceiw Guest

    Thomas Tutone 写é“:
    > Squeamizh wrote:
    >
    >>kceiw wrote:
    >>
    >>>How to make a member function inline while the declaration and
    >>>implementation are separated?

    >>
    >>If you want to keep the declaration and body separate, then the easiest
    >>way to do it is by puttin the function body in the same header file as
    >>the class definition, but outside the actual class.
    >>
    >>E.G.:
    >>
    >>--- BEGIN EXAMPLE.H ----
    >>class Example {
    >>public:
    >> Example();
    >> inline void helloWorld() const;
    >>};
    >>
    >>void Example::helloWorld() const {
    >> std::cout << "hello world";
    >>}
    >>--- END EXAMPLE.H ---

    >
    >
    > Your example wouldn't result in the member function being declared
    > inline, as the OP specified. I think perhaps you meant something like:
    >
    > class Example {
    > public:
    > void memFunc() const;
    > };
    >
    > inline void Example::memFunc() const
    > {
    > doSomething();
    > }
    >
    > Best regards,
    >
    > Tom
    >


    How about when the inline function are put outside the header file? When
    I try to do it, it make an error while linking.
    Do I have to put the function body in the header file when I want to
    make it inline? Dose it differ when I define the function while it's
    declared?
     
    kceiw, Sep 7, 2005
    #5
  6. kceiw

    tmartsum Guest

    2 possibilities:

    1) Then it is (probably) not inlined.
    The function is however in 2 more objectfiles.

    2) You implemented the function inline in
    file1.cpp
    and
    file2.cpp uses the inline function, but how
    should file2.cpp see a function that should be inline and exists in
    file1.cpp

    gcc has an option to force export of inlined functions, but ofcourse
    they
    will then NOT be inlined in file2.cpp
     
    tmartsum, Sep 7, 2005
    #6
  7. kceiw

    Gabriel Guest

    <scissors applied>
    >
    > How about when the inline function are put outside the header file? When
    > I try to do it, it make an error while linking.
    > Do I have to put the function body in the header file when I want to
    > make it inline? Dose it differ when I define the function while it's
    > declared?


    As long as the declaration is visible when you call the function, you
    should not get a linker error.

    It does not make a differnce if you have separate definition/declaration
    from a declaration/definition in one place. However, if the
    _definition_ is not visible when you call the function, there is high
    chance that the function will in fact not be inlined (This is very
    difficult for the compiler/linker).

    GAbriel
     
    Gabriel, Sep 7, 2005
    #7
    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. Nish
    Replies:
    4
    Views:
    545
    Thomas Stegen
    Oct 8, 2004
  3. TGOS
    Replies:
    3
    Views:
    403
    Kevin Bracey
    Feb 28, 2005
  4. Ajay
    Replies:
    5
    Views:
    425
    Pete Becker
    Apr 1, 2006
  5. Replies:
    7
    Views:
    464
Loading...

Share This Page