CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2009
    Posts
    9

    Question Inline (strange behavior)

    hello to everyone,

    1)
    if i declare a function in a header file like this :

    void Foo() { /* code here*/}

    the linker will fail due to multiple definition !!! (Foo already defined in Myclass.obj)

    2)

    but if i declare it like this :

    inline void Foo() { /* code here*/}

    it compiles normally.

    So here is the questions :

    a) since i declared the function in the header file, isnt it by default inline ?
    Why do i have to specify the inline keyword again ?

    b) what if a compiler decides to ignore the inline keyword ? will it result in multiple definition during link time ?

    thank you,
    Last edited by anonymous12345; February 11th, 2010 at 03:35 PM. Reason: typo

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Inline (strange behavior)

    since i declared the function in the header file, isnt it by default inline ?
    no.

    Why do i have to specify the inline keyword again ?
    To ask the compiler to expand the function inline... although the compiler can decide otherwise.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Inline (strange behavior)

    Quote Originally Posted by anonymous12345 View Post
    b) what if a compiler decides to ignore the inline keyword ? will it result in multiple definition during link time ?
    No. The compiler is smart enough to figure out that you intended it to be an inline function even if it doesn't end up being treated that way. It can inform the linker to treat it as having file-scope linkage even if it remains a separate function (similar to a static function), which means the linker won't have trouble with it.

  4. #4
    Join Date
    Jul 2009
    Posts
    9

    Smile Re: Inline (strange behavior)

    i see, thanks, everything is clear now !!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured