Home > Backend Development > C++ > body text

Can Inline Member Functions Be Implemented in a .cpp File?

Susan Sarandon
Release: 2024-10-31 18:36:29
Original
597 people have browsed it

 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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!