如何声明和实施const和内联成员函数
How to declare and implement a const and inline member function?
代码:
point3f.h
Class Point3f {
...
inline void project2D(ProjType p, const Point2i& view) const;
};
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;
}
}
调用此功能会在编译时间中引起错误:
undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'
我尝试了没有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'|
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'|
inline
不在标题中,而是在CPP中:
undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|
inline
不在标题中,cpp中都不是:
It works but that's not what I want
问题:
-
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;
}
}
};
在这种情况下,根本不需要inline
关键字。如果定义类定义中的功能,则 inline
是默认值。但是,如果您愿意,您仍然可以指定它(如我在上面的示例中所做的那样。)
im测试此问题并正常工作!可以在以下网址查看此示例:http://www.doc.ic.ac.uk/lab/cplus/c rules/chap7.html
示例24:相对于const-ness
超载操作员/函数 #include <iostream.h>
#include <string.h>
static unsigned const cSize = 1024;
class InternalData {};
class Buffer
{
public:
Buffer( char* cp );
// Inline functions in this class are written compactly so the example
// may fit on one page. THIS is NOT to be done in practice (See Rule 21).
// A. non-const member functions: result is an lvalue
char& operator[]( unsigned index ) { return buffer[index]; }
InternalData& get() { return data; }
// B. const member functions: result is not an lvalue
char operator[]( unsigned index ) const { return buffer[index]; }
const InternalData& get() const { return data; }
private:
char buffer[cSize];
InternalData data;
};
inline Buffer::Buffer( char* cp )
{
strncpy( buffer , cp , sizeof( buffer ) );
}
main()
{
const Buffer cfoo = "peter";// This is a constant buffer
Buffer foo = "mary";// This buffer can change
foo[2]='c';// calls char& Buffer::operator[](unsigned)
cfoo[2] = 'c' // ERROR: cfoo[2] is not an lvalue.
// cfoo[2] means that Buffer::operator[](unsigned) const is called.
cout << cfoo[2] << ":" << foo[2] << endl; // OK! Only rvalues are needed
foo.get() = cfoo.get();
cfoo.get() = foo.get(); // ERROR: cfoo.get() is not an lvalue
}
希望!
和平与光!
您将其声明为cpp文件中的内联,因此在point3f.cpp中没有发出符号。但是包括标题在内的其他文件,无法包含函数,它们需要发出此符号。我想在这里就是这样。
相关文章:
- 不允许运算符 const 参数调用 const 成员函数
- c++ 是否保证标头初始化的静态 const 成员跨编译单元和库共享单个实例?
- 在类声明中初始化 const 成员变量时在调试模式下出现异常
- 如何避免将 const 和非 const 成员函数输入到模板中的代码重复
- 初始化 const 成员的正确方法
- 使用 Boost 对具有 const 成员的类进行序列化
- 非常量调用 const 成员函数失败,只读位置C++
- PIMPL类的设置应该是const成员函数
- 与其他静态const成员初始化静态常量成员
- decltype 不会推断 const 对象的 const 成员
- 如何初始化不是静态的const成员
- 如何使用常量变量初始化类的 const 成员变量
- 使用模板参数初始化静态 const 成员
- 与const成员配对的向量
- 通过参考const成员通过参考时,尝试引用已删除的函数
- const成员初始化之前的用法是GCC和Clang的这种预期行为
- 通过const成员变量访问内联函数
- 与const成员一起安排新的和班级的分配
- 在const函数中调用非CONST成员的非const函数
- 为什么即使对于 "copy on write" 的 const 成员函数也返回一个代理类?