Objects and Classes in C++: Inline Functions in Classes

You can define short functions as inline functions to improve performance.

Section 6.10, “Inline Functions,” introduced how to improve function efficiency using inline functions. When a function is implemented inside a class definition, it automatically becomes an inline function. This is also known as inline definition. For example, in the following definition for class A, the constructor and function f1 are automatically inline functions, but function f2 is not.

class A

{

public:

A()

{

// Do something;

}

double f1()

{

// Return a number

}

double f2();

}

There is another way to define inline functions for classes. You may define inline functions in the class’s implementation file. For example, to define function f2 as an inline function, precede the inline keyword in the function header as follows:

// Implement function as inline

inline double A::f2()

{

// Return a number

}

As noted in Section 6.10, short functions are good candidates for inline functions, but long functions are not.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *