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,
Re: Inline (strange behavior)
Quote:
since i declared the function in the header file, isnt it by default inline ?
no.
Quote:
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.
Re: Inline (strange behavior)
Quote:
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.
Re: Inline (strange behavior)
i see, thanks, everything is clear now !!!