C++. Inline member functions of class. The inline keyword




Inline member functions of class. The inline keyword


Contents


Search other websites:

1. What functions are called inline functions?

In C++, two types of functions can be used in classes:

  • functions that are substituted or built-in functions. Such functions are also called inline functions;
  • ordinary functions, the code of which is not substituted directly into the body of the calling program.

Inline-functions are handled just like a macro. When you call such a function, the function’s body is directly inserted into this code. In other words, the inline function code is substituted at the place of the program line from which it is called. As a result, calling an inline function gives a better execution time of the program (the processing time of the function). This is due to the fact that the overhead costs for additional processing disappear when passing (receiving) parameters to a function.

2. What are the ways to declare (create) inline functions in a class definition?

There are two ways to declare (create) an inline function.

Way 1. By using the ‘inline’ modifier. The general form of the function declared with the inline modifier is:

inline returned_type FunName(parameters)
{
    // ...
}

where

  • returned_type – the type returned by the function;
  • parameters – parameters of function

Not all compilers support this method.

Way 2. Implementing the code of the member function of the class directly in the body of the class declaration. In this case, the use of the inline modifier is optional.

The general view of a class with declared inline functions is as follows:

class CMyClass
{
    // ...

    // without 'inline' modifier
    returned_type MyInlineFun1(parameters)
    {
        // ...
    }

    // using 'inline' modifier
    inline returned_type MyInlineFun2(parameters)
    {
        // ...
    }

    // ...
}

where

  • returned_type – the type returned by the function;
  • parameters – parameters received by the function.

3. An example of declaring a class that contains inline functions

The example declares the CMyPoint class. The class contains internal data members and also the inline function Get(). Two other functions of the class are ordinary, since the implementation of these functions is outside the class.

// class that contains inline-functions
class CMyPoint
{
    int x, y;

    public:
    // constructor of class
    CMyPoint(void);

    int GetX(void); // ordinary (not inline) function of the class

    inline int GetY(void) // inline-function
    {
        return y; // implementation in the body of a class
    }

    // ordinary (not inline) function
    void SetXY(int nx, int ny);
};

// class constructor
CMyPoint::CMyPoint(void)
{
    x = y = 0;
}

// implementation of not-inline function
int CMyPoint::GetX(void)
{
    return x;
}

// implementation of ordinary (not inline) function
void CMyPoint::SetXY(int nx, int ny)
{
    x = nx;
    y = ny;
}

Using a class in another program code (for example, an event handler)

CMyPoint MP1; // A class object that contains an inline function

// call an ordinary (non inline) function
MP1.SetXY(25,30);

int tx, ty;
tx = MP1.GetX(); // call not inline-function
ty = MP1.GetY(); // call inline-function

4. What are the advantages of using inline functions?

The main advantage of using inline functions is the acceleration of program execution time.

This is due to the fact that when you call an inline function, you do not waste time:

  • writing arguments to the stack;
  • reading arguments from the stack when returning from the function.





5. In which cases is it appropriate to declare functions with an ‘inline’ modifier?

Add an ‘inline’ modifier to a function declaration is advisable in cases where two basic conditions are met:

  • the function call occurs so often that it negatively affects the execution speed of the program or simply significantly slows the execution of the program. For example, a function can be called repeatedly in a loop statement;
  • the size of function code is small. Functions that have large size of code, significantly increase the size of the program itself, which is also sometimes undesirable. Therefore, as inline, it is recommended to use only very small functions in size.

6. What actions does the compiler do when declaring an inline function?

The declaration of an inline function is a request and not a command. Therefore, the compiler may not execute the request to generate code for the declared inline function. In this case, the function can be used as ordinary (not inline).

For example:

  • in most cases, recursive functions can not be used as inline functions;
  • you can not generate an inline function that contains static data members.

7. Reasons that can lead to ignoring the inline keyword

In some cases, the compiler cannot define a function as inline and simply ignores the inline keyword. Possible reasons for this compiler behavior are listed below:

  1. The size of the function is too large.
  2. The function is recursive.
  3. A function can be repeated several times in the same expression.
  4. The function uses control statements switch, if, or loop statements.


Related topics