inline functions

Inline function features:
  • Equivalent to an inline function call to write the content inside inline function at;
  • Equivalent function without executing step proceeds directly execute functions thereof;
  • Equivalent to macro, macro more than type checking, truly function characteristic;
  • Compiler does not generally contain inline loop, recursion, inline functions like a complicated operation switch;
  • Functions defined in the class declaration, among other functions will automatically virtual function implicitly as inline functions.

Advantages and disadvantages

  advantage

  1. Inline function like macro function will be invoked at the code being launched, eliminating the parameters on the stack and the stack frame open recovered result is returned, so as to increase the running speed.
  2. Inline function compared to macro functions, in the code when deployed, will do security checks or automatic type conversion (with normal function), and macro definition does not.
  3. Declared in the class member functions defined in the same time, automatically converted into an inline function, so inline functions can access the class member variable, the macro can not be defined.
  4. Inline function at run-time debugging, and macro definitions can not.

  Shortcoming

  1. Code bloat. Inline is code bloat (copy) the cost of eliminating the overhead of a function call brings. If the execution time of the function body of code, compared to the function call overhead is large, the efficiency gains will be very limited. On the other hand, every call within inline function must copy the code, the total amount of code the program will increase, consume more memory space.
  2. inline function can not be upgraded as the library upgrade. Change the inline functions need to be recompiled, unlike non-inline direct link.
  3. Whether inline, programmers uncontrollable. Inline functions just suggest to the compiler, whether for function inlining, the decision rests with the compiler
Virtual function (virtual) can be inline (inline) do?
  • Virtual function can be inline, inline virtual functions can be modified, but when virtual functions can not be polymorphic inline.
  • Inline is a compiler compiler to inline the recommendation, and polymorphism virtual functions at runtime, the compiler can not know which code to run on the call, so the performance when virtual function polymorphism (runtime) can not be inline.
  • inline virtual The only time you can within the Union are: the compiler knows which object is called class (such as  Base::who()), this will occur only with a pointer or reference to the actual object rather than the object of the compiler.
 
#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 () 
{ 
    // virtual function here WHO (), by the class (Base) specific objects (b) to the call, can be determined during compilation, so it may be inline, but ultimately it depends on whether inlining compiler. 
    B Base; 
    b.who (); 

    // here the virtual function is called through a pointer, showing polymorphism, in order to determine the need during runtime, it is not inline.  
    * PTR = Base new new the Derived (); 
    PTR -> WHO (); 

    // because Base has a virtual destructor (virtual ~ Base () {} ), so that when the delete, will first call the derived class (the Derived) destructor , and then calls the base class (base) destructor, memory leaks. 
    Delete PTR; 
    PTR = nullptr a; 

    System ( " PAUSE " );
     return  0 ; 
}

Reference links: https://github.com/huihut/interview# advantages and disadvantages

 

Guess you like

Origin www.cnblogs.com/single-dont/p/11421028.html