inline only in defination, why??

This topic is 4484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 on other sites
I guess my question is not clear enough. What I was asking is, where should we write the keyword inline...In declaration, In defination or Both?

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 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.

Share on other sites
What are you trying to explain my dear. plz read my question properly.
You are explaining things which I am not asking.

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 on other sites
Quote:
 Original post by CTarIf 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

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• Forum Statistics

• Total Topics
631415
• Total Posts
2999966
×