C++ Design/Coding tips - Part 2
2. Use of Inline functions

in

Inline functions are a request to the compiler telling it to replace the function call by actual implementation of that function. But its not guaranteed that inline request would be satisfied. It depends on many other parameters. The request may be rejected in following situation:
-  Recursive call
-  When its virtual function
-  When address of inline function is used

Don't encourage yourself for making constructors and destructors as inline functions because, inline functions which invoke inline functions and so on become often too complex for compilers to be able to make them inline even if they are small in size. Especially, constructors that invoke the constructors of its base classes and its member data before executing its own code. And most of the time we will make our destructors as virtual, so compiler will reject our request for inline. So making them as inline may mislead others.

And never implement (definition) inline functions in .cpp (source) files. Either give implementation to them at the end of the header, or define them in some .inl (inline) file and include this file at the end of that header.

Problem with keeping implementation of inlines in .cpp file is, some other compilation unit that is using this inline function wont gets the implementation (definition) of the inline function.

//SomeHeader.h
#ifndef SOMEHEADER_H
...
inline void SomeFunction();
void SomeTestFunction();
...
#endif //SOMEHEADER

//SomeCpp.Cpp
#include “SomeHeader.h”
...
void SomeFunction()
{
...
}
void SomeTestFunction()
{
...
}
...

//SomeOtherCpp.Cpp
#include “SomeHeader.h”
void SomeOtherFunction ()
{
SomeFunction();
SomeTestFunction();
...
}
...

In the above code, I have declared SomeFunction() as inline and SomeTestFunction() as normal function. Implementations of both functions are at SomeCpp.Cpp. SomeOtherCp.cpp is making use of these functions. But in this case, while building the Project, linker will throw a error telling unresolved symbol SomeFunction() (But not for SomeTestFunction()).

So, the solution is, keep definition of inline function at the end of the file or, move it to some .inl file and include it at the end of the header file. And remove it from the cpp file :

//SomeHeader.h
#ifndef SOMEHEADER_H
...
inline void SomeFunction();
void SomeTestFunction();
...
#include “SomeHeader.inl”
#endif //SOMEHEADER_H

//SomeHeader.inl
#ifndef SOMEHEADER_INL
void SomeFunction()
{
...
}
#endif //SOMEHEADER_INL

Tutorial posted September 1st, 2005 by girishshetty categories [ ]

> C++ Design/Coding tips - Part 2

"inline" should be used in function definition near body not in declaration in header... Isn't it true????

> C++ Design/Coding tips - Part 2

>>"inline" should be used in function definition near body not in declaration in header... Isn't it true????

Sorry, it was by mistake. I have missed inline keyword in the defination :-(

And Thanks for that observation and informing me.

Regards Girish

> C++ Design/Coding tips - Part 2

Goog to know. In a moment I hold that symbian gcc compiler use some other rules:)

BTW: Good articles Wojtek