Skip to content

Last updated:2026-10-12

C++ inline Functions

About the Author

WeChat official account:

wechat

1. Inline Inside a Class

Declare the methods in the header file

class A
{
public:
    void f1(int x); 

    /**
     * @brief 类中定义了的函数是隐式内联函数,声明要想成为内联函数,必须在实现处(定义处)加inline关键字。
     *
     * @param x
     * @param y
     */
    void Foo(int x,int y) ///< 定义即隐式内联函数!
    {

    };
    void f2(int x); ///< 声明后,要想成为内联函数,必须在定义处加inline关键字。  
};

Define inline functions in the implementation file:

#include <iostream>
#include "inline.h"

using namespace std;

/**
 * @brief inline要起作用,inline要与函数定义放在一起,inline是一种“用于实现的关键字,而不是用于声明的关键字”
 *
 * @param x
 * @param y
 *
 * @return 
 */
int Foo(int x,int y);  // 函数声明
inline int Foo(int x,int y) // 函数定义
{
    return x+y;
}

// 定义处加inline关键字,推荐这种写法!
inline void A::f1(int x){

}

int main()
{


    cout<<Foo(1,2)<<endl;

}
/**
 * 编译器对 inline 函数的处理步骤
 * 将 inline 函数体复制到 inline 函数调用点处;
 * 为所用 inline 函数中的局部变量分配内存空间;
 * 将 inline 函数的的输入参数和返回值映射到调用方法的局部变量空间中;
 * 如果 inline 函数有多个返回点,将其转变为 inline 函数代码块末尾的分支(使用 GOTO)。
 */

Inlining can improve a function's efficiency, but not every function should be defined as inline! Inlining comes at the cost of code bloat (duplication); it merely saves the overhead of the function call, thereby improving the function's execution efficiency.

  • If the time spent executing the code in the function body is large compared with the overhead of the function call, the efficiency gain will be smaller!

  • On the other hand, every call to an inline function duplicates code, which increases the program's total code size and consumes more memory space.

Inlining should not be used in the following cases:

(1)If the code in the function body is fairly long, inlining leads to a comparatively high memory-consumption cost.

(2)If a loop appears in the function body, the time spent executing the code in the function body exceeds the overhead of the function call.

2. Can a Virtual Function (virtual) Be an Inline Function (inline)?

  • A virtual function can be an inline function; inline may qualify a virtual function, but it cannot be inlined when the virtual function exhibits polymorphism.
  • Inlining is a compile-time suggestion to the compiler, while the polymorphism of virtual functions happens at run time; the compiler cannot know which code gets called at run time, so a virtual function cannot be inlined when it behaves polymorphically (at run time).
  • inline virtual can be inlined only when the compiler knows which class the called object belongs to (e.g. Base::who()); this happens only when the compiler has an actual object rather than a pointer or reference to the object.
#include <iostream>  
using namespace std;
class Base
{
public:
    inline virtual void who()
    {
        cout << "I am Base\n";
    }
    virtual ~Base() {}
};
class Derived : public Base
{
public:
    inline void who()  // 不写inline时隐式内联
    {
        cout << "I am Derived\n";
    }
};

int main()
{
    // 此处的虚函数 who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。 
    Base b;
    b.who();

    // 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。  
    Base *ptr = new Derived();
    ptr->who();

    // 因为Base有虚析构函数(virtual ~Base() {}),所以 delete 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。
    delete ptr;
    ptr = nullptr;


    return 0;
} 

Comments