[C++] What are Inline Functions? | How to Implement Member Functions Inside Class Definitions

目次

Introduction

In C++ classes, it is common practice to “declare (prototype)” member functions inside the class definition and “implement (process content)” them outside the class. However, for functions with very short processing, writing the declaration and implementation separately can feel a bit redundant. In such cases, you can write the processing content of the member function directly inside the class definition. Functions defined this way are treated as “Inline Functions” by the compiler, potentially improving performance. In this article, I will explain how to implement member functions inline and the difference from the normal (non-inline) implementation method.

Sample Code for Inline and Normal Functions

This code defines two types of member functions inside the Product class.

  • getID(): Implemented inside the class definition as an inline function.
  • display(): Implemented outside the class definition as a normal function.

Complete Code

#include <iostream>
#include <string>

using namespace std;

class Product {
private:
    int id;
    string name;

public:
    // Constructor
    Product(int i, string n) {
        id = i;
        name = n;
    }

    // --- 1. Inline Function ---
    // Describe the processing content inside the class definition
    int getID() {
        return id;
    }
    
    // --- 2. Normal Member Function (Prototype Declaration Only) ---
    void display();
};

// --- Implementation of Normal Member Function ---
void Product::display() {
    cout << "ID: " << id << ", Product Name: " << name << endl;
}


int main() {
    Product item(101, "High-Performance Mouse");
    
    // Call normal member function
    item.display();
    
    // Call inline function
    cout << "Retrieved ID: " << item.getID() << endl;
    
    return 0;
}

Code Explanation

1. Inline Function (getID)

int getID() {
    return id;
}

The getID function is fully described, including its processing content { return id; }, inside the definition block {} of the Product class. Functions written this way become a hint (instruction) to the compiler: “Please inline expand this function.”

What is Inline Expansion? Normally, when a function is called, program execution jumps to the location where the function body exists, and returns to the original location when processing is finished. This jump incurs a very small cost. For inline functions, the compiler may replace the “call” code directly with the function’s processing content itself. This eliminates the cost of function calls and may improve performance.

2. Normal Member Function (display)

Like void display();, only the function prototype is declared inside the class definition, and the actual processing content is implemented outside the class like void Product::display() { ... }. This is the standard implementation method for C++ member functions.

When to Use Inline Functions

Inlining reduces the overhead of function calls, but since the code is expanded, overuse has the disadvantage of increasing the size of the final executable file. As a rule of thumb, inline functions are generally limited to very short functions that finish in 1-2 lines, like getID(). “Getters” that only return the value of a private member variable, or “Setters” that only set a value, are good candidates for inline functions.

Summary

In this article, I explained the basic usage of C++ inline functions.

  • Member functions implemented with processing content inside the class definition become inline functions.
  • Function call costs are reduced, potentially improving performance.
  • It is generally limited to use in very short functions (getters, setters, etc.).

Inline functions are one means of optimizing performance, but it is important to use them appropriately from the perspective of readability and file size.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次