关于在C++头文件中定义成员函数

对于Java来说,class文件中包含了成员函数的定义,但是,对于C++来说,一般将成员函数的声明放在头文件,而将其实现放在.cpp里面,那么在头文件中写成员函数的具体实现是否可以呢?

The C++ ISO standard says:

"A function defined within a class definition is an inline function."

But, this doesn't mean the function will necessarily be inlined: generally nowadays, it appears that the compiler will decide if inlining the function will lead to any benefits.

也就是说,C++标准规定了此类函数会被视为内联函数,但是具体实现要根据编译器而定,如果编译器没有将其视为内联函数,那么头文件被多个源文件包含以后,会出现多处定义,在链接的时候会报错。

猜你喜欢

转载自blog.csdn.net/sundongsdu/article/details/79498087