Reprinted from light – city. The club/sc/basic_co…
Preface:
Inline functions are copied to the call location instead of jumping to the call location, which avoids the extra time consumed by the function call itself and makes caches easier to hit.
1. Inline in the class
The header declares:
class A
{
public:
void f1(int x);
The /** * @brief class defines functions that are implicitly inline, stating that to be inline, the implementation (definition) must be inline. * * @param x * @param y */
void Foo(int x,int y) ///< define that implicitly inline function!
{};void f1(int x); After the < declaration, to become an inline function, you must add the inline keyword to the definition.
};
Copy the code
Inline functions defined in the implementation file:
#include <iostream>
#include "inline.h"
using namespace std;
/** * @brief inline to work,inline goes with the function definition,inline is a "keyword for implementation, not declaration" ** @param x * @param y ** @return */
int Foo(int x,int y); // Function declaration
inline int Foo(int x,int y) // Function definition
{
return x+y;
}
// Add inline to the definition.
inline void A::f1(int x){}int main(a)
{
cout<<Foo(1.2)<<endl;
}
/** * The compiler handles inline functions by copying the body of an inline function to the call point of the inline function; * Allocate memory space for local variables in the used inline function; * Map the input parameters and return values of an inline function to the local variable space of the calling method; * If an inline function has more than one return point, convert it to a branch at the end of the code block of the inline function (using GOTO). * /
Copy the code
Inlining can improve function efficiency, but not all functions are defined as inline functions! Inlining is at the expense of code bloat (copy) and simply eliminates the overhead of function calls, thus improving the efficiency of function execution.
If the time spent executing the code inside the function is more expensive than the function call, the efficiency gains will be even less!
On the other hand, copying code for every inline function call increases the total amount of code in the program and consumes more memory.
Do not use inline when:
(1) If the code inside the function is long, inlining will lead to high memory consumption cost.
(2) If there is a loop in the function body, then the time to execute the code in the function body is more expensive than the function call.
2. Can virtual functions be inline functions?
- Virtual functions can be inlined, which can modify virtual functions, but cannot be inlined when virtual functions are polymorphic.
- Inlining is when the compiler advises the compiler to inline, whereas polymorphisms of virtual functions are at run time, and the compiler has no way of knowing which code is called at run time, so virtual functions that appear to be polymorphic (run time) cannot be inlined.
- The only time inline Virtual can be inlined is when the compiler knows which class the object being called is (such as Base::who()), which only happens if the compiler has a pointer or reference to the actual object rather than the object.
#include <iostream>
using namespace std;
class Base
{
public:
inline virtual void who(a)
{
cout << "I am Base\n";
}
virtual ~Base() {}};class Derived : public Base
{
public:
inline void who(a) // Do not write inline implicitly
{
cout << "I am Derived\n"; }};int main(a)
{
// The virtual function who(), which is called from the concrete object (b) of class (Base), is determined at compile time, so it can be inline, but ultimately it depends on the compiler.
Base b;
b.who(a);// The virtual function here is called through a pointer, is polymorphic and needs to be determined at runtime, so it cannot be inline.
Base *ptr = new Derived(a); ptr->who(a);// The virtual ~Base() {} destructor (virtual ~Base() {}) is used for delete. The Derived destructor is called first and then the Base destructor is called to prevent memory leaks.
delete ptr;
ptr = nullptr;
system("pause");
return 0;
}
Copy the code