将内联方法从头文件移动到.cpp文件 [英] Moving inline methods from a header file to a .cpp files

查看:123
本文介绍了将内联方法从头文件移动到.cpp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 foo.h 头文件中定义了以下类。

I have the following class defined in a foo.h header file

class Foo {

public:
    inline int Method();

};

inline int Foo::Method() { // Implementation }

我现在想把实现移动到 foo.cpp 文件。为此,我必须删除 inline 关键字并将该方法的实现移动到 foo.cpp 文件像这样

I would like now to move the implementation to a foo.cpp file. To this end, I have to remove the inline keyword and move the implementation of the method to a foo.cpp file like this

#include `foo.h`

inline int Foo::Method() { // Implementation }

我有两个问题:


  1. 我的声明是关于删除 inline 关键字是否正确? c> inline 关键字如何移除通常会影响性能(几乎所有的方法都是内联的)?

  1. Is my statement about the removal of the inline keyword correct? Should it be necessarily removed?
  2. How typically the removal of the inline keyword affect the performance (practically all my methods are inlined)?

非常感谢您提前。

推荐答案

如果将函数定义从头文件移动到cpp文件,则必须删除 inline 关键字all 所有功能。使用旧版链接器可能会使事情稍微慢一点,但是使用现代链接器,您应该注意到性能上没有真正的差异。 *

If you moved the function definition from a header to a cpp file, you MUST remove the inline keyword all all locations for that function. With older linkers it might make things slightly slower, but with modern linkers you should notice no real difference in performance.*

在某些情况下,公共成员函数可以 inline ,但这只是一个坏主意。不要这样做。参数可以用于标记某些私有成员函数为 inline ,但实际上你真正想要的是 __ attribute __((always_inline)) __ forceinline

There are certain cases where a public member function can be inline, but that's just a bad idea. Don't do it. Arguments can be made for marking certain private member functions as inline, but in reality what you really want in those to be __attribute__((always_inline)) or __forceinline

*在极少数情况下,但99%的时间它不会,和99.9%的什么离开你不在乎。如果测量结果显示您达到一万分之一,您可以使用前述的 __ forceinline

这篇关于将内联方法从头文件移动到.cpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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