|
-
February 11th, 2010, 03:34 PM
#1
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
-
February 11th, 2010, 04:07 PM
#2
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.
-
February 11th, 2010, 04:31 PM
#3
Re: Inline (strange behavior)
 Originally Posted by anonymous12345
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.
-
February 11th, 2010, 05:12 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|