Pure virtual destructor in C++

本文探讨了C++中纯虚析构函数的概念及其使用场景,解释了为何需要为纯虚析构函数提供定义,并通过实例展示了不提供定义时的编译错误。此外,还讨论了纯虚析构函数如何使类成为抽象类,并强调了其在强制派生类覆盖中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Do not be anxious about anything, but in everything, 
by prayer and petition, with thanksgiving, present 
your requests to god. 
Philippians 4:6 (Bible)

Can a destructor be pure virtual in C++?
Yes, it is possible to have pure virtual destructor. Pure virtual destructor are legal in standard C++ and one of the most important thing is that if class contains pure virtual destructor it is must to provide a function body for the pure virtual destructor. This seems strange that how a virtual function is pure if it requires a function body? But destructors are always called in the reverse order of the class derivation. That means derived class destructor will be invoked first & then base class destructor will be called. If definition for the pure virtual destructor is not provided then what function body will be called during object destruction? Therefore compiler & linker enforce existence of function body for pure virtual destructor.
Consider following program:

#include <iostream>
class Base
{
public:
    virtual ~Base()=0; // Pure virtual destructor
};

class Derived : public Base
{
public:
    ~Derived()
    {
        std::cout << "~Derived() is executed";
    }
};

int main()
{
    Base *b=new Derived();
    delete b;
    return 0;
}

The linker will produce following error in the above program.

test.cpp:(.text$_ZN7DerivedD1Ev[__ZN7DerivedD1Ev]+0x4c): 
undefined reference to `Base::~Base()' 

Now if the definition for the pure virtual destructor is provided then the program compiles & runs fine.

#include <iostream>
class Base
{
public:
    virtual ~Base()=0; // Pure virtual destructor
};
Base::~Base()
{
    std::cout << "Pure virtual destructor is called";
}

class Derived : public Base
{
public:
    ~Derived()
    {
        std::cout << "~Derived() is executed\n";
    }
};

int main()
{
    Base *b = new Derived();
    delete b;
    return 0;
}

Output:

~Derived() is executed
Pure virtual destructor is called

It is important to note that class becomes abstract class when it contains pure virtual destructor. For example try to compile the below program.

#include <iostream>
class Test
{
public:
    virtual ~Test()=0; // Test now becomes abstract class
};
Test::~Test() { }

int main()
{
    Test p;
    Test* t1 = new Test;
    return 0;
}

The above program fails in compilation & shows following error messages.
[Error] cannot declare variable ‘p’ to be of abstract type ‘Test’
[Note] because the following virtual functions are pure within ‘Test’:
[Note] virtual Test::~Test()
[Error] cannot allocate an object of abstract type ‘Test’
[Note] since type ‘Test’ has pure virtual functions

Pure virtual destructor seems to be useless but it is actually not. Pure virtual destructor in a base class forces the derived class to override them. Following is conversation between me & Dr. Bjarne Stroustrup about this topic via mail.

Me: When pure virtual destructor is required?
Dr. Stroustrup: Pure virtual destructor is useful for the usual reason: to force a derived class to override them. You just have to remember to define it also.

Sources:
http://www.bogotobogo.com/cplusplus/virtualfunctions.php
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf

This article is contributed Meet Pravasi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值