c ++内联函数_C ++中的内联函数

c ++内联函数

All the member functions defined inside the class definition are by default declared as Inline. Let us have some background knowledge about these functions.

默认情况下,在类定义中定义的所有成员函数都声明为Inline。 让我们对这些功能有一些背景知识。

You must remember Preprocessors from C language. Inline functions in C++ do the same thing what Macros did in C language. Preprocessors/Macros were not used in C++ because they had some drawbacks.

您必须记住C语言中的预处理器。 C ++中的内联函数与Macros在C语言中所做的事情相同。 C ++中未使用预处理器/宏,因为它们有一些缺点。

C ++中预处理器/宏的缺点 (Drawbacks of Preprocessors/Macros in C++)

In Macro, we define certain variable with its value at the beginning of the program, and everywhere inside the program where we use that variable, its replaced by its value on Compilation.

在Macro中,我们在程序开始处以及在我们使用该变量的程序中到处使用该值定义了某些变量,该变量在Compilation中被其值替换。

1)间距问题 (1) Problem with spacing)

Let us try to understand this problem using an example,

让我们尝试通过一个例子来理解这个问题,

#define G (y) (y+1)

Here we have defined a Macro with name G(y), which is to be replaced by its value, that is (y+1) during compilation. But, what actually happens when we call G(y),

在这里,我们定义了一个名称为G(y)的宏,该宏将由其值替换,即编译期间的(y+1) 。 但是,当我们调用G(y)时,实际发生了什么

G(1)  // Macro will replace it

the preprocessor will expand it like,

预处理器会像这样扩展它,

(y) (y+1) (1)

You must be thinking why this happened, this happened because of the spacing in Macro definition. Hence big functions with several expressions can never be used with macro, so Inline functions were introduced in C++.

您必须在想为什么会这样,这是由于Macro定义中的间距而发生的。 因此,带有多个表达式的大型函数永远不能与宏一起使用,因此Inline函数是在C ++中引入的。

2)复杂的论题 (2) Complex Argument Problem)

In some cases such Macro expressions work fine for certain arguments but when we use complex arguments problems start arising.

在某些情况下,此类Macro表达式对于某些参数可以正常工作,但是当我们使用复杂的参数时,就会出现问题。

#define MAX(x,y) x>y?1:0

Now if we use the expression,

现在,如果我们使用表达式,

if(MAX(a&0x0f, 0x0f))  // Complex Argument

Macro will Expand to,

宏将扩展为

if( a&0x0f > 0x0f ? 1:0)

Here precedence of operators will lead to problem, because precedence of & is lower than that of >, so the macro evaluation will surprise you. This problem can be solved though using parenthesis, but still for bigger expressions problems will arise.

在这里,运算符的优先级会导致问题,因为&优先级低于>优先级,因此宏评估会使您感到惊讶。 尽管可以使用括号解决此问题,但是对于较大的表达式,仍然会出现问题。

3)无法访问班级私人成员 (3) No way to access Private Members of Class)

With Macros, in C++ you can never access private variables, so you will have to make those members public, which will expose the implementation.

使用宏,在C ++中,您永远无法访问私有变量,因此必须使这些成员成为公共成员,从而公开实现。

class Y
{
    int x;
    public : 
    #define VAL(Y::x)   // Its an Error
}

C ++中的内联函数 (Inline Functions in C++)

Inline functions are actual functions, which are copied everywhere during compilation, like preprocessor macro, so the overhead of function calling is reduced. All the functions defined inside class definition are by default inline, but you can also make any non-class function inline by using keyword inline with them.

内联函数是实际函数,在编译期间会像预处理器宏一样复制到各处,因此可以减少函数调用的开销。 默认情况下,在类定义中定义的所有函数都是内联的,但是您也可以通过使用关键字inline来使任何非类函数内

For an inline function, declaration and definition must be done together. For example,

对于内联函数,必须一起进行声明和定义。 例如,

inline void fun(int a) 
{ 
    return a++; 
}

关于内联函数的一些重要点 (Some Important points about Inline Functions)

  1. We must keep inline functions small, small inline functions have better efficiency.

    我们必须保持内联函数较小,较小的内联函数具有更好的效率。

  2. Inline functions do increase efficiency, but we should not make all the functions inline. Because if we make large functions inline, it may lead to code bloat, and might affect the speed too.

    内联函数确实可以提高效率,但是我们不应该使所有函数内联。 因为如果我们使大型函数内联,则可能导致代码膨胀 ,也可能影响速度。

  3. Hence, it is adviced to define large functions outside the class definition using scope resolution :: operator, because if we define such functions inside class definition, then they become inline automatically.

    因此,建议使用范围解析::运算符在类定义之外定义大型函数,因为如果我们在类定义内部定义此类函数,则它们将自动变为内联。

  4. Inline functions are kept in the Symbol Table by the compiler, and all the call for such functions is taken care at compile time.

    内联函数由编译器保存在符号表中,并且在编译时会仔细考虑对此类函数的所有调用。

C ++中的Getter和Setter函数 (Getter and Setter Functions in C++)

We have already studied this in the topic accessing private data variables inside a class. We use access functions, which are inline to do so.

我们已经在访问类中的私有数据变量的主题中对此进行了研究。 我们使用内联的访问功能。

class Auto
{
    // by default private
    int price;
    
    public:
    // getter function for variable price
    int getPrice()
    {
        return price;
    }
    // setter function for variable price
    void setPrice(int x)
    {
        i=x;
    }
};

Here getPrice() and setPrice() are inline functions, and are made to access the private data members of the class Auto. The function getPrice(), in this case is called Getter or Accessor function and the function setPrice() is a Setter or Mutator function.

这里的getPrice()setPrice()是内联函数,用于访问Auto类的私有数据成员。 函数getPrice()在这种情况下称为Getter或Accessor函数,而setPrice()函数是Setter或Mutator函数。

There can be overlaoded Accessor and Mutator functions too. We will study overloading functions in next topic.

也可以覆盖Accessor和Mutator函数。 我们将在下一个主题中研究重载函数。

内联函数的局限性 (Limitations of Inline Functions)

  1. Large Inline functions cause Cache misses and affect performance negatively.

    大型内联函数会导致高速缓存未命中,并对性能产生负面影响。

  2. Compilation overhead of copying the function body everywhere in the code on compilation, which is negligible for small programs, but it makes a difference in large code bases.

    在编译时将代码主体复制到任何地方的编译开销,对于小程序可以忽略不计,但是对于大型代码库则有所不同。

  3. Also, if we require address of the function in program, compiler cannot perform inlining on such functions. Because for providing address to a function, compiler will have to allocate storage to it. But inline functions doesn't get storage, they are kept in Symbol table.

    另外,如果我们需要程序中函数的地址,则编译器无法对此类函数执行内联。 因为要为函数提供地址,编译器将不得不为其分配存储。 但是内联函数不获取存储,它们保留在Symbol表中。

了解C ++中的前向引用 (Understanding Forward References in C++)

All the inline functions are evaluated by the compiler, at the end of class declaration.

所有内联函数都由编译器在类声明的末尾求值。

class ForwardReference
{
    int i;
    public:
    // call to undeclared function
    int f() 
    {
        return g()+10;
    }
    int g() 
    {
        return i;
    }
};

int main()
{
    ForwardReference fr;
    fr.f();
}

You must be thinking that this will lead to compile time error, but in this case it will work, because no inline function in a class is evaluated until the closing braces of class declaration.

您必须考虑到这将导致编译时错误,但在这种情况下它将起作用,因为在类声明的右括号之前,不会评估类中的任何内联函数。

翻译自: https://www.studytonight.com/cpp/inline-functions.php

c ++内联函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值