inline Function & Member Function

In C++ Primer:
 
inline Function:
   The inline specification is only a request to the compiler. The compiler may choose to ignore this request.
   ...
   Unlike other function definitions, inlines should be defined in header files.
   To expand the code of an inline function at the point of call, the compiler must have access to the function definition. The function prototype is insufficient.(p.257)
inline Member Function:
class Screen {
public:
...
inline
char get(index ht, index wd) const;
...
index get_cursor()
const;
};
// inline declared in the class declaration; no need to repeat on the definition
char Screen::get(index r, index c) const
{
...
}
// not declared as inline in the class declaration, but ok to make inline in definition
inline Screen::index Screen::get_cursor() const
{
...
}
   The definition for an inline member function that is not defined within the class body ordinarily should be placed in the same header file in which the class definition appears.(p.437)
   总之, 凡是 inline 函数, 都应该放在头文件中. 在 class 中的 inline 函数, 无论是在 declaration 或是在 definition引用inline, 那么此函数都是 inline.
posted @ 2011-05-14 20:21  walfud  阅读(389)  评论(0编辑  收藏  举报