内联成员函数和普通成员函数有什么区别? [英] What's the difference between inline member function and normal member function?

查看:60
本文介绍了内联成员函数和普通成员函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内联成员函数(内联函数主体)和其他普通成员函数(在单独的.cpp文件中的函数主体)之间是否有区别?

Is there any difference between inline member function (function body inline) and other normal member function (function body in a separate .cpp file)?

例如,

class A
{
  void member(){}
};

// Header file (.hpp)

class B
{
  void member();
};

// Implementation file (.cpp)

void B::member(){}

推荐答案

绝对没有区别.

两者之间的唯一区别是类中的成员被隐式标记为内联.但这没有真正的意义.

The only difference between the two is that the member inside the class is implicitly tagged as inline. But this has no real meaning.

请参阅:内联和良好做法

文档说,内联标记是向编译器(由开发人员)提示应该内联方法的提示.所有现代的编译器都忽略了此提示,并使用自己的内部启发式方法来确定何时应内联方法(因为众所周知,人类非常糟糕并做出了此决定).

The documentation says that the inline tag is a hint to the compiler (by the developer) that a method should be inlined. All modern compilers ignore this hint and use there own internal heuristic to determine when a method should be inlined (As humans are notoriously bad and making this decision).

内联的另一种用法是,它告诉链接器它可能期望看到一个方法的多个定义.当函数定义位于头文件中时,每个获取头文件的编译单元都将具有该函数的定义(假定未内联).通常,这将导致链接器生成错误.借助inline标记,编译器可以理解为什么会有多个定义,并将从应用程序中删除除一个以外的所有定义.

The other use of inline is that it tells the linker that it may expect to see multiple definitions of a method. When the function definition is in the header file each compilation unit that gets the header file will have a definition of the function (assuming it is not inlined). Normally this would cause the linker to generate errors. With the inline tag the compiler understands why there are multiple definitions and will remove all but one from the application.

有关内联过程的说明:不需要在头文件中内联方法.现代的编译器具有对整个应用程序进行优化的过程,其中可以考虑所有函数进行内联,即使它们已在不同的编译单元中进行了编译.由于通常会忽略内联标志,因此,如果将方法放在标头或源文件中,则没有任何区别.

Note on inlining the processes: A method does not need to be in the header file to inlined. Modern compilers have a processes a full application optimization where all functions can be considered for inlining even if they have been compiled in different compilation units. Since the inline flag is generally ignored it make no difference if you put the method in the header or the source file.

这篇关于内联成员函数和普通成员函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆