C++ 类内的内联函数成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9370493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:44:42  来源:igfitidea点击:

inline function members inside a class

c++classmethods

提问by AlexDan

I know that declaring a function (normal function not a method inside a class) as inline is a good practice when the function definition is small for performance and it save time for the compilation. But how about inline methods inside a class I don't understand the concept of inline methods inside a class? How to define them and how they work.

我知道,当函数定义对于性能来说很小并且可以节省编译时间时,将函数(普通函数而不是类中的方法)声明为内联是一种很好的做法。但是类内的内联方法怎么样?我不理解类内内联方法的概念?如何定义它们以及它们如何工作。

回答by Alexander Poluektov

but how about inline methods inside a class ?

但是类中的内联方法呢?

Both syntaxes for inlining functions (using explicit inlineand defining member-function inside class definition) provides only hint about inlining for compiler. From performancepoint of view, they are equal.

内联函数的两种语法(inline在类定义中使用显式和定义成员函数)仅提供有关编译器内联的提示。从性能的角度来看,它们是平等的。

In case of defining a member-function inside a class declaration, the readabilityof the latter should be of your main concern: it really hurts to litter class interface with multiple line of implementation details. So avoid doing that if your member-function is more than one statement: return stuffor simple forwarding should be OK, but usually no more than that.

在类声明中定义成员函数的情况下,后者的可读性应该是您的主要关注点:用多行实现细节乱丢类接口真的很伤人。因此,如果您的成员函数不止一个语句,请避免这样做:return stuff或者简单的转发应该没问题,但通常不会更多。

class MyClass
{
public:
    int f() const { return m_i; }
    int g() const;

private:
    int m_i;
};

inline int MyClass::g() const
{
    return m_i;
}

// both member-functions behave equally (except for naming)

回答by John

Specifying a function/procedure as inlineinside a class is hinting to the compiler that instead of creating code to call the function and pass parameters, the contents of the function should be placed at the point of call.

将函数/过程指定为inline类内部是在暗示编译器,而不是创建代码来调用函数和传递参数,函数的内容应该放在调用点。

It can improve performance of the compiled binary when it becomes more efficient to execute the function without having to pass parameters. It can also be a detriment to performance because repeating the code that would have been in the function at every call location can cause bloat which lessens the liklihood that your code will be found in faster cache memory.

当无需传递参数即可更高效地执行函数时,它可以提高已编译二进制文件的性能。这也可能对性能造成损害,因为在每个调用位置重复函数中的代码会导致膨胀,从而减少在更快的缓存中找到您的代码的可能性。

回答by Jan Koester

There are two options to offer to the compiler to make a class function inline:

有两个选项可以提供给编译器来使类函数内联:

(1) Defining a function in the declaration of the class(in a header file)

(1)在类声明中定义一个函数(在一个头文件中)

class Human {

public:

    Human(const char* name);
    Human();

    // is implicit inline
    void lookAt(const char* name) const {
        std::cout << "I'm looking at " << name << std::endl;

    }

private:
    char _name[30]; 

}; 

(2) Using the inline keyword explicitly in the definition of the function (in a header file)

(2) 在函数定义中显式使用 inline 关键字(在头文件中)

    // is explicit inline 
    inline void lookAt(const char* name) const {
        std::cout << "I'm looking at " << name << std::endl;

    }