成员函数定义中的内联关键字 [英] Inline keyword in member function definition

查看:115
本文介绍了成员函数定义中的内联关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要在成员函数的定义中使用inline关键字。不在声明中?

Why inline keyword should used in the definition of member function. and Not in declaration?

推荐答案

inline ,但现在最好记住它说:这个定义将被定义多次,这是可以的。

inline has some pre-historic use, but nowadays it's best to remember it as saying: "this definition is going to be defined multiple times, and that's okay."

也就是说,通常单一定义规则禁止函数的多个定义。这:

That is, normally the one-definition rule prohibits multiple definitions of a function. This:

// foo.hpp
void foo() { /* body */ }

// a.cpp
#include "foo.hpp"

// b.cpp
#include "foo.hpp"

会导致错误,因为 foo 在两个翻译单元中定义。您可以随时随地声明内容。这:

results in an error, as foo is defined in two translation units. You can declare things as often as you want. This:

// foo.hpp
void foo();

// foo.cpp
void foo()
{
    /* body */
}

// a.cpp
#include "foo.hpp"

// b.cpp
#include "foo.hpp"

很好,因为 foo 定义一次,并且声明多次。 inline 是允许这样的:

is fine, as foo is defined once, and declared multiple times. What inline does is allow this:

// foo.hpp
inline void foo() { /* body */ }

// a.cpp
#include "foo.hpp"

// b.cpp
#include "foo.hpp"

工作。它说如果你看到 foo 多次,只是假设它们是一样的,并且可以与它。

to work. It says "if you see foo more than once, just assume they are the same and be okay with it".

这篇关于成员函数定义中的内联关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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