Michael van der Werve
2018-10-11 15:09:45 UTC
Currently, you need to *explicitly *specify the destructor as virtual, even
if all methods are pure virtual.
Example which does not work correctly.
class Base
{
public:
// should be overridden
virtual void doSomething() = 0;
// define destructor
virtual ~Base() = default;
};
class Derived : public Base
{
public:
// override the function
virtual void doSomething() override {}
// define the destructor
virtual ~Derived()
{
// do some fancy destructing
std::cout << "destructed" << std::endl;
}
};
And then running the following code
// create a derived instance
Derived *derived = new Derived();
// cast it to the base
Base *base = derived;
// now delete it as the base
delete base;
In the example, the derived class destructor will not be called. It can be
solved by simply altering the base class as such.
class Base
{
public:
// should be overridden
virtual void doSomething() = 0;
// add a virtual destructor
virtual ~Base() = default;
};
And naturally, everything works as expected.
So let me ask you this, is there any reason that a virtual destructor
should not simply be assumed when the class only has pure virtual
functions? There will be a virtual method table anyway, so why could it not
be assumed then?
Iâd propose to make it the default, where you *can* make it non-virtual by
specifying it explicitly if you want to disallow deleting by a pointer for
example. Wouldn't that make more sense?
if all methods are pure virtual.
Example which does not work correctly.
class Base
{
public:
// should be overridden
virtual void doSomething() = 0;
// define destructor
virtual ~Base() = default;
};
class Derived : public Base
{
public:
// override the function
virtual void doSomething() override {}
// define the destructor
virtual ~Derived()
{
// do some fancy destructing
std::cout << "destructed" << std::endl;
}
};
And then running the following code
// create a derived instance
Derived *derived = new Derived();
// cast it to the base
Base *base = derived;
// now delete it as the base
delete base;
In the example, the derived class destructor will not be called. It can be
solved by simply altering the base class as such.
class Base
{
public:
// should be overridden
virtual void doSomething() = 0;
// add a virtual destructor
virtual ~Base() = default;
};
And naturally, everything works as expected.
So let me ask you this, is there any reason that a virtual destructor
should not simply be assumed when the class only has pure virtual
functions? There will be a virtual method table anyway, so why could it not
be assumed then?
Iâd propose to make it the default, where you *can* make it non-virtual by
specifying it explicitly if you want to disallow deleting by a pointer for
example. Wouldn't that make more sense?
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/17fe64b2-7c78-492e-814d-fef72e3a668b%40isocpp.org.
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/17fe64b2-7c78-492e-814d-fef72e3a668b%40isocpp.org.