[Bug c++/20560] New: RFE: -Wnon-virtual-dtor/-Wall should warn when a class appears to be polymorphic and there is no destructor at all

andyreif at studcs dot uni-sb dot de gcc-bugzilla@gcc.gnu.org
Sat Mar 19 20:59:00 GMT 2005


At the moment it only warns, if there _is_ a non-virtual destructor: 
 -Wnon-virtual-dtor (C++ only) 
           Warn when a class appears to be polymorphic, thereby requiring a 
virtual destructor, yet 
           it declares a non-virtual one.  This warning is enabled by -Wall. 
 
It doesn't warn if it was forgotten to declare a destructor. (But the compiler 
creates a non-virtual one implicitly). 
Are there reasons not to warn about it? 
 
#include <iostream>  
 
struct A {  
    // assume it is forgotten, you don't get any warnings about that:  
//     virtual ~A() {}  
    virtual void justDoIt() = 0;  
};  
  
struct B : public A {  
    int i;  
    void justDoIt() { i = 5; }  
  
    virtual ~B() { std::cout << i << std::endl; }  
};  
  
int main() {  
    B b;  
    b.justDoIt();  
  
    A* a = new B;  
    a->justDoIt();  
    // B destructor won't be called, since A's destructor is not virtual  
    delete a;  
}  
 
Output: 
5

-- 
           Summary: RFE: -Wnon-virtual-dtor/-Wall should warn when a class
                    appears to be polymorphic and there is no destructor at
                    all
           Product: gcc
           Version: 3.3.5
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: andyreif at studcs dot uni-sb dot de
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20560



More information about the Gcc-bugs mailing list