内联定义和声明 [英] inline in definition and declaration

查看:158
本文介绍了内联定义和声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的同行评审期间,另一位软件工程师建议我声明 inline 函数 inline 定义(在类外部)以及在声明(在类内部)。他的论据是通过标记它的内联,你说这个方法将比非内联方法执行得更快,调用者不必担心过多的方法调用。

During a recent peer review, another Software Engineer suggested that I state that the inline function is inline in the definition (outside of the class) as well as in the declaration (inside of the class). His argument is that "By marking it inline, you are saying that this method will be executed much faster than a non-inline method, and that callers don't have to worry about excessive calls to the method."

这是真的吗?如果我是一个类的用户,我真的关心对方法的过多调用吗?在定义和声明中将它列为内联有什么问题吗? C ++常见问题说明:

Is that true? If I am a user of a class, do I really care about excessive calls to the method? Is there anything wrong with listing it as inline in both the definition and declaration? The C++ FAQ states:


最佳实践:只在类体外的定义。

Best practice: only in the definition outside the class body.

是在这里?

推荐答案

这听起来像两个完全不相干的东西。将 inline 置于类的声明和类外部的定义不是必需的。把它放在一个声明就足够了。

That sounds like two totally unrelated things. Putting inline at both the declaration in the class and the definition outside of the class is not needed. Putting it at one of the declarations is enough.

如果您谈到在 .cpp inline >文件中定义函数,并且该函数是 public 成员,那么你应该这样做。调用 inline 函数的人员必须能够看到他们的定义。 C ++标准要求。

If you talk about adding inline in a .cpp file where a function is defined there, and that function is a public member, then you should not do that. People who call inline functions must have their definitions visible to them. The C++ Standard requires that.

这篇关于内联定义和声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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