Quoting from C++ FAQ:
Note: It's imperative that the function's definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file. In particular, if you put the inline function's definition into a .cpp file and you call it from some other .cpp file, you'll get an "unresolved external" error from the linker.
The compiler need to see the definition of the inline function whenever it finds any use of that inline function. That is typically possible if the inline function is placed in a header file.
Will the compiler inline getA?
No, except when the the use of getA() is in B.cpp itself.
If so, which inline keyword is the significant one (the one in the header or the one in the cpp)?
Best practice: only in the definition outside the class body.
Is there another way to put the definition of an inline member function into it's cpp file?
No, at least I don't know.
本文探讨了C++中内联函数的正确使用方法。强调了内联函数定义必须放在头文件中的重要性,避免链接错误。同时解答了关于内联关键字的有效位置以及内联成员函数定义方式的问题。
448

被折叠的 条评论
为什么被折叠?



