• Cpp Chapter 8: Adventures in Functions Part1


    //第一次在博客上发自己的C++ primer plus学习笔记,从现在进度(第8章)开始,用英文也当是练练英语写作了

    1. Inline Functions

    ) When calling a regular function, the program execution will be transfered to the called function and back, thus making it time-consuming under circumstances that call the same short function for a multiple of times. In an inline function, the compiler replaces the function call with corresponding function code, so the program execution don't need to be transfered.

    ) If the function code's execution time is short relatively to the execution-transfer process, using inline function will save a great proportion of time.

    ) To implement this feature, common practice is to omit the function prototype and put the entire definition at the beginning of the file with keyword inline.

    for example:

     1 // example of inline function comparison
     2 #include <iostream>
     3 using namespace std;
     4 double square(double);
     5 
     6 int main()
     7 {
     8 ...
     9 }
    10 
    11 double sqaure(double x)
    12 {
    13     return x * x;
    14 }

    this code uses a function called square. It has only one line of code, so transfer it to inline function:

    // example of inline function comparison
    #include <iostream>
    using namespace std;
    inline double square(double x) {return x * x;}
    
    int main()
    {
    ...
    }

    By adding the inline keyword, you mean that square() is an inline function.

    ) Back in C, it used #define methods to cope with similar problems. But #define is simply a text substitution, inline functions can pass arguments by value just like normal functions do.

    2. Reference Variables

    ) A reference is a name that acts as an alternative name for a previously defined variable. The main use is as a formal argument to a function so the function could work with the original data, unlike the passing-by-value fashion.

    ) C++ use '&' for declaring references:

    int rats;
    int & rodents = rats;

    In this way, "int &" is an identifier means reference to int, so rodents is a reference to rats and they are exactly the same, no matter value or address.

    ) One should initialize a reference variable when declare it. Unlike pointers, reference doesn't support later assignment.

    int rats;
    int & rodents = rats;
    int bunnies;
    rodents = bunnies;

    The last line above doesn't make rodents reference to bunnies, but it actually works as "rats = bunnies".

    ) Take the function swap() which swaps two values for example, using reference in functions(compared to pointers and value):

    void swapr(int & a, int & b); // function prototype for reference
    swapr(a, b) // function call for reference
    void swapp(int * a, int * b); // function prototype for pointer
    swapp(&a, &b) // function call for pointer
    void swapv(int a, int b); // function prototype for value
    swapv(a, b) // function call for value

     

  • 相关阅读:
    perl 添加主机
    java将阿拉伯数字转换为中文数字
    使用EL调用Java方法
    Perl 检索zabbix 主机
    perl和curl 模拟post 发送json数据
    Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.continuation.ContinuationThrowable
    3.4. JVM Agent
    perl JMX::Jmx4Perl::Manual 说明
    perl 安装 JMX::Jmx4Perl 需要版本5.10.1
    Chapter 2.Jolokia Architecture 结构:
  • 原文地址:https://www.cnblogs.com/fsbblogs/p/9692278.html
Copyright © 2020-2023  润新知