简体   繁体   English

扩展boost :: iterator_facade时,如何删除有关迭代器具有非虚拟析构函数的警告?

[英]How can I remove the warning that my iterator has a non-virtual destructor when extending boost::iterator_facade?

When compiling with -Weffc++ and extending boost::iterator_facade, I get the compiler warning: base class has a non-virtual destructor. 当使用-Weffc ++进行编译并扩展boost :: iterator_facade时,我得到了编译器警告:基类具有非虚拟析构函数。 What can I do to fix this? 我该怎么做才能解决此问题?

Here is sample code: 这是示例代码:

#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;
}

I compile on Fedora 19 like this: 我像这样在Fedora 19上编译:

$ g++ test.cpp -std=gnu++0x -Weffc++ -o test

Here is the actual warning: 这是实际的警告:

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<
       ^

Thanks. 谢谢。

-Weffc++ option enables warnings about violations of the some style guidelines from Scott Meyers' Effective C++ book. -Weffc ++选项启用有关违反Scott Meyers的Effective C ++书籍中的某些样式准则的警告。 Your code violates the Item 7: Make destructors virtual in polymorphic base classes. 您的代码违反了条款7:在多态基类中使析构函数虚拟化。 So the compiler isn't complaining about your iterator, it's about the base class: boost::iterator_facade. 因此,编译器不会抱怨您的迭代器,而是关于基类的:boost :: iterator_facade。 I don't think you can eliminate the warning by modify your own code. 我认为您无法通过修改自己的代码来消除警告。 As to why virtual destructor in polymorphic base classes are so important, a good answer is here . 关于为什么多态基类中的虚拟析构函数如此重要, 这里有一个很好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM