使用 -Weffc++ 编译并扩展 boost::iterator_facade 时,我收到编译器警告:基类具有非虚拟析构函数。我能做些什么来解决这个问题?
这是示例代码:
#include <iostream>
#include <boost/iterator/iterator_facade.hpp>
struct my_struct_t {
int my_int;
my_struct_t() : my_int(0) {
}
};
class my_iterator_t : public boost::iterator_facade<
my_iterator_t,
my_struct_t const,
boost::forward_traversal_tag
> {
private:
friend class boost::iterator_core_access;
my_struct_t my_struct;
public:
my_iterator_t() : my_struct() {
}
void increment() {
++ my_struct.my_int;
}
bool equal(my_iterator_t const& other) const {
return this->my_struct.my_int == other.my_struct.my_int;
}
my_struct_t const& dereference() const {
return my_struct;
}
};
int main() {
my_iterator_t my_iterator;
std::cout << my_iterator->my_int << "\n";
++my_iterator;
std::cout << my_iterator->my_int << "\n";
return 0;
}
我在 Fedora 19 上编译如下:
$ g++ test.cpp -std=gnu++0x -Weffc++ -o test
这是实际的警告:
g++ test.cpp -std=gnu++0x -Weffc++ -o test
test.cpp:10:7: warning: base class ‘class boost::iterator_facade<my_iterator_t, const my_struct_t, boost::forward_traversal_tag>’ has a non-virtual destructor [-Weffc++]
class my_iterator_t : public boost::iterator_facade<
^
谢谢。