I just had a discussion with a coworker concerning code in header files:
He says that code defined in header files will always be inlined by the compiler (like the code from the function GetNumber() in my example header). I say it will be inlined sometimes, whenever the compiler decides to do so. So which one of us has to bring a cake to work for telling filthy lies? Or maybe we are both wrong…?
MyClass.hpp
class MyClass
{
public:
MyClass();
~MyClass();
int GetNumber() const
{
//...;
return m_number;
};
private:
int m_number;
};
Any function defined within the class (like your GetNumber example) rather than just declared is implicitly
inline. What that means is that it’s equivilent to using theinlinekeyword, so multiple inclusions of the header will not cause link errors due to multiple definitions of those functions.Most modern compiler treat
inlineas a linkage command and nothing more. Some compilers provide stronger keywords such as CL’s__forceinlinewhich mean ‘inline this if it’s possible to do so’.So you’re both right and both wrong to a degree.