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

StackOverflow https://stackoverflow.com/questions/21198907

  •  29-09-2022
  •  | 
  •  

Pergunta

When compiling with -Weffc++ and extending boost::iterator_facade, I get the compiler warning: base class has a non-virtual destructor. 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:

$ 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.

Foi útil?

Solução

-Weffc++ option enables warnings about violations of the some style guidelines from Scott Meyers’ Effective C++ book. Your code violates the Item 7: Make destructors virtual in polymorphic base classes. So the compiler isn't complaining about your iterator, it's about the base class: 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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top