Home > Article > Backend Development > Can Inline Member Functions Be Implemented in a .cpp File?
Inline Member Functions in C : A Deeper Dive
Inline member functions are declared in the class definition but implemented outside the class body. While they typically reside in the header file, there may be situations where placing the implementation in the header is not feasible.
Consider the scenario where class A includes an instance of class B, and class B declares an inline member function getA() that returns an instance of A. Due to a circular include, we're forced to move the implementation of getA() to the B.cpp file.
The question arises: will the compiler inline getA()? According to the C FAQ, the definition of an inline function must be in a header file, except if it's used exclusively within a single .cpp file. In our case, the definition is in B.cpp, which means getA() will not be inlined except for its usage within B.cpp itself.
Regarding the significance of the inline keywords, it's recommended to only use the one in the function definition outside the class body for best practices.
As for finding an alternative approach to defining inline member functions in a .cpp file, unfortunately, there isn't one known to our knowledge. The definition must reside in a header file for the compiler to perform inlining.
The above is the detailed content of Can Inline Member Functions Be Implemented in a .cpp File?. For more information, please follow other related articles on the PHP Chinese website!