Sign in to follow this  
TEUTON

inline only in defination, why??

Recommended Posts

Why is it prefered to write inline only in defination but not in declaration?? I was going through cline FAQ but couldn'y understand his language. Can anyone make it clear for me.

Share this post


Link to post
Share on other sites
Every .cpp file in your project gets compiled. Those .cpp files probably in turn #include .h files.

Now the whole nature of inline is to, instead of making a function call, insert the function body.

If a second .cpp file wants the inlined function then the linker has to go to the first, find the function, and then give it back to you.

But if your function body was in the .h file, which both of the .cpps included, your linker wouldn't have to do a thing. The function body will be there when both .cpps get compiled.

Share this post


Link to post
Share on other sites
When used like this (below) your code is inline. You don't need the keyword.

class blah
{
public:

int GetFoo() const { return m_Foo; }
void SetFoo( const int Foo ) { m_Foo = Foo; }

private:

int m_Foo;
};


Otherwise, you would use the keyword. However the other situations + keyword is only a hint to the compiler. There is no promise made. Even with things like __FORCEINLINE or whatever. An example is:

class blah
{
inline int GetFoo() const;
};

int blah::GetFoo() const
{
return m_Foo;
}

Share this post


Link to post
Share on other sites
As a general rule of thumb, you only want to use that first option I showed, where the code literally is inlined into the declaration. And you only want to do it when the body of the function is extremely small (like those one-liners). Constructors and destructors don't count here since they do a bunch behind the scenes.

You can read more info about it at codingstandard.com.

Share this post


Link to post
Share on other sites
If you are refering to [9.9] then I think the author explains it quite clear. The declaration and definition have different purposes. The declaration is meant to give an overview of your class, people should be able to read your declaration and use your class. The definition is meant to describe how what was explained in the declaration is working. Inline tells how a function should "work" (be called). Inline should not be in the declaration since it doesn't give any info about the actual declaration, it gives info about the implementation.

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
If you are refering to [9.9] then I think the author explains it quite clear. The declaration and definition have different purposes. The declaration is meant to give an overview of your class, people should be able to read your declaration and use your class. The definition is meant to describe how what was explained in the declaration is working. Inline tells how a function should "work" (be called). Inline should not be in the declaration since it doesn't give any info about the actual declaration, it gives info about the implementation.

yaa...that is what I was asking...but i couldn't properly understand cline's language...you made it clear now.
Thank You

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this