Inline Functions in C++
Inline Functions in C++
1
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Inline that
resource utilization. This means Functions
when theininline
C++ function body is
substituted at the point of the function call, the total number of variables
used by the function also gets inserted. So the number of registers going to
be used for the variables will also get increased. So if after function inlining
variable numbers increase drastically then it would surely cause overhead on
register utilization.
2. If you use too many inline functions then the size of the binary executable
file will be large, because of the duplication of the same code.
3. Too much inlining can also reduce your instruction cache hit rate, thus
reducing the speed of instruction fetch from that of cache memory to that of
primary memory.
4. The inline function may increase compile time overhead if someone changes
the code inside the inline function then all the calling location has to be
recompiled because the compiler would be required to replace all the code
once again to reflect the changes, otherwise it will continue with old
functionality.
5. Inline functions may not be useful for many embedded systems. Because in
embedded systems code size is more important than speed.
6. Inline functions might cause thrashing because inlining might increase the
size of the binary executable file. Thrashing in memory causes the
performance of the computer to degrade. The following program
demonstrates the use of the inline function.
Example:
#include <iostream>
using namespace std;
inline int cube(int s) { return s * s * s; }
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0
C++ also allows you to declare the inline functions within a class. These functions
need not be explicitly defined as inline as they are, by default, treated as inline
functions. All the features of inline functions also apply to these functions.
However, if you want to explicitly define the function as inline, you can easily do
that too. You just have to declare the function inside the class as a normal function
and define it outside the class as an inline function using the inline keyword.
#include <iostream>
using namespace std;
// a class to find the area
class findArea
{
public:
// declare functions
// (implicitly inline functions)
void circle(int radius);
void square(int side);
3
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Inline Functions in C++
void triangle(int base, int height);
void rectangle(int length, int breadth);
};
int main()
4
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
{ Inline Functions in C++
cout << "Inline function with classes program\n\n";
// create an object of the class
findArea A;
// call the inline functions
A.circle(2);
A.square(5);
A.triangle(3, 4);
A.rectangle(10, 12);
cout << "\n\n";
return 0;
}