码:
point3f.h
Class Point3f {
...
inline void project2D(ProjType p, const Point2i& view) const;
};
Run Code Online (Sandbox Code Playgroud)
point3f.cpp
inline void Point3f::project2D(ProjType p, const Point2i& view) const {
switch(p) {
case PROJ_XY:
glVertex2f(x * view.x, y * view.y);
break;
case PROJ_YZ:
glVertex2f(y * view.x, z * view.y);
break;
case PROJ_XZ:
glVertex2f(x * view.x, z * view.y);
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
调用此函数会在编译时引发错误:
undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'
Run Code Online (Sandbox Code Playgroud)
我试过没有和用inline符号的每个案例:
inline 在标题中,而不是在cpp中:
Warning: inline function ‘void Point3f::project2D(ProjType, const Point2i&) const’ used but never defined [enabled by default
undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|
Run Code Online (Sandbox Code Playgroud)
inline 在标题中,也在cpp中:
Warning: inline function ‘void Point3f::project2D(ProjType, const Point2i&) const’ used but never defined [enabled by default
undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|
Run Code Online (Sandbox Code Playgroud)
inline 不是在标题中,而是在cpp中:
undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|
Run Code Online (Sandbox Code Playgroud)
inline 不在标题中,也不在cpp中:
It works but that's not what I want
Run Code Online (Sandbox Code Playgroud)
题:
const and inline member function有意义?const and inline member function?提前致谢.
功能const与它无关.如果需要inline,则必须在头文件中而不是在头文件中定义它point3f.cpp.例:
class Point3f {
...
inline void project2D(ProjType p, const Point2i& view) const
{
switch(p) {
case PROJ_XY:
glVertex2f(x * view.x, y * view.y);
break;
case PROJ_YZ:
glVertex2f(y * view.x, z * view.y);
break;
case PROJ_XZ:
glVertex2f(x * view.x, z * view.y);
break;
default:
break;
}
}
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,inline根本不需要关键字.如果在类定义中定义函数,inline则为默认值.但是如果你愿意,你仍然可以指定它(正如我在上面的例子中所做的那样).