zoukankan      html  css  js  c++  java
  • Inline Functions in C++

    Although you've already learned about basic functions in c++, there is more: the inline function. Inline functions are not always important, but it is good to understand them. The basic idea is to save time at a cost in space. Inline functions are a lot like a placeholder. Once you define an inline function, using the 'inline' keyword, whenever you call that function the compiler will replace the function call with the actual code from the function.



    How does this make the program go faster? Simple, function calls are simply more time consuming than writing all of the code without functions. To go through your program and replace a function you have used 100 times with the code from the function would be time consuming not too bright. Of course, by using the inline function to replace the function calls with code you will also greatly increase the size of your program.

    Using the inline keyword is simple, just put it before the name of a function. Then, when you use that function, pretend it is a non-inline function.

    Example Inline Function

     
    #include <iostream>
    
    using namespace std;
    
    inline void hello()
    { 
      cout<<"hello";
    }
    int main()
    {
      hello(); //Call it like a normal function...
      cin.get();
    }
    
    However, once the program is compiled, the call to hello(); will be replaced by the code making up the function.

    A WORD OF WARNING: Inline functions are very good for saving time, but if you use them too often or with large functions you will have a tremendously large program. Sometimes large programs are actually less efficient, and therefore they will run more slowly than before. Inline functions are best for small functions that are called often.

    Finally, note that the compiler may choose, in its infinite wisdom, to ignore your attempt to inline a function. So if you do make a mistake and inline a monster fifty-line function that gets called thousands of times, the compiler may ignore you.
  • 相关阅读:
    Genymotion安装与集成开发指南
    近期遇到的Android问题解决与总结
    关于Android Studio乱码的解决办法
    菜鸟程序员如何才能快速提高自己的技术
    android不能调试解决方法
    导入工程出现@Override错误
    读取本地文件的权限问题
    混合app开发
    JNI 实战全面解析
    Android性能优化
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/2101595.html
Copyright © 2011-2022 走看看