在C ++中的内联函数 [英] Inline functions in C++

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

问题描述

Hii,



我是C ++的新手。我读过关于内联函数并理解它们。但是网站说我们得到了一个未解决的外部错误,如果我们写一个内联函数的定义在一个.cpp文件,并从另一个文件中调用....为什么是这样...?
这可以做正常的功能...请正确我如果我错了...



感谢

解决方案

inline 表示您可以在多个翻译单元中定义函数,但定义必须相同,并且

这些是规则 (但是 不要求)编译器在每个调用点扩展 inline 函数的代码,并省略发出可调用的函数版本。



这与非 - inline 函数不同,这些函数只能在所有翻译单元中定义一次。这是通常的一个定义规则,适用于C ++中的大多数实体。



inline 连接一个函数。 inline 函数默认具有外部链接,所以如果在中使用 static inline 函数的实现必须确保程序中只有该变量的一个副本。


Hii ,

I am a novice in C++. I did read about inline functions and understood them right. But this site says that "We get an 'unresolved external' error if we write the definition of an inline function in one .cpp file and call it from another file....why is that so ... ? This can be done for normal functions right...Please correct me if i am wrong ...

Thanks

解决方案

It's a language requirement. inline means that you may have the function defined in more than one translation unit but the definitions must be identical and that you must have a definition in every translation unit that uses the function.

Those are the rules. The rules allow (but don't require) the compiler to expand the code for the inline function at each call site and omit emitting a callable function version.

This is different from non-inline functions which must only be defined once across all translation units. This is the usual "one definition rule" which applies to most entities in C++.

inline doesn't change the linkage of a function. inline functions have, by default, external linkage so if you use a static variable inside an inline function the implementation must ensure that there is only one copy of that variable in the program.

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

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