Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to call the "deleting destructor" of a pure virtual class?

I'm using C++11 and g++4.8 on Ubuntu Trusty.

Consider this snippet

class Parent {
public:
    virtual ~Parent() =  default;
    virtual void f() = 0;
};

class Child: public Parent {
public:
    void f(){}
};

Called using

{
    Child o;
    o.f();
}
{
    Parent * o  = new Child;
    delete o;
}
{
    Child * o  = new Child;
    delete o;
}

I use gcov to generate my code coverage report. It report that the destructor with symbol _ZN6ParentD0Ev is never called, while _ZN6ParentD2Ev is.

Answer Dual emission of constructor symbols and GNU GCC (g++): Why does it generate multiple dtors? reports that _ZN6ParentD0Ev is the deleting constructor.

Is there any case where this "deleting destructor" is called on the Parent class ?

Subsidiary question: if not, is there a way to get the gcov/lcov code coverage tool (used following answer of Detailed guide on using gcov with CMake/CDash?) ignore that symbol in its report ?

like image 738
rcomblen Avatar asked Sep 04 '14 09:09

rcomblen


People also ask

Can a virtual destructor be manually called?

No, destructors are called automatically in the reverse order of construction. (Base classes last). Do not call base class destructors. you can't have a pure virtual destructor without a body.

Does a pure virtual class need a destructor?

While defining (providing an implementation) pure virtual methods is rarely useful, you must define a pure virtual destructor. This is because the destructor of a base class is always called when a derived object is destroyed. Failing to define it will cause a link error.

What is vector deleting destructor?

The vector deleting destructor takes some flags. If 2 is set, then a vector is being destructed; otherwise a single object is being destructed. If 1 is set, then the memory is also freed. In our case, the flags parameter is 3, so we will perform a vector destruct followed by a delete.

Can we have virtual destructor and pure virtual destructor?

Yes, it is possible to have a pure virtual destructor. Pure virtual destructors are legal in standard C++ and one of the most important things to remember is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor.


1 Answers

You can't have Parent objects, so no. It's a GCC oversight that this needless function is generated. The optimizer really should remove it, as it's unused, but I've found that GCC has problems is that area as well.

like image 91
MSalters Avatar answered Oct 05 '22 12:10

MSalters