关于类的虚拟析构函数、纯虚拟析构函数

#include<iostream>
using namespace std;

// 1st pair base & derived
struct Node
{
    Node()  {cout<<"This is constructor of Node."<<endl;};
    ~Node() {cout<<"This is destructor of Node."<<endl;};
};
struct MathNode: public Node
{
    MathNode()  {cout<<"This is constructor of MathNode."<<endl;};
    ~MathNode() {cout<<"This is destructor of MathNode."<<endl;};
};

// 2nd pair base & derived
struct Node_2
{
    Node_2()  {cout<<"This is constructor of Node_2."<<endl;};
    virtual ~Node_2() {cout<<"This is destructor of Node_2."<<endl;}; // virtual function
};
struct MathNode_2: public Node_2
{
    MathNode_2()  {cout<<"This is constructor of MathNode_2."<<endl;};
    ~MathNode_2() {cout<<"This is destructor of MathNode_2."<<endl;};
};

// 3rd pair base & derived
struct Node_3
{
    Node_3()  {cout<<"This is constructor of Node_3."<<endl;};
    virtual ~Node_3() {cout<<"This is destructor of Node_3."<<endl;}; 
    virtual void foo() = 0; // pure virtual function to make the class abstract
};
struct MathNode_3: public Node_3
{
    MathNode_3()  {cout<<"This is constructor of MathNode_3."<<endl;};
    ~MathNode_3() {cout<<"This is destructor of MathNode_3."<<endl;};
};

int main()
{
    cout<<"Example 1"<<endl;
    MathNode* a = new MathNode; // type: MathNode*
    delete a; // derived class destructor is called, and then the base class's

    cout<<"Example 2"<<endl;
    Node* b = new MathNode; // type: Node*
    delete b; // only base class destructor is called

    cout<<"Example 3"<<endl;
    Node_2* c = new MathNode_2; // type: Node_2*
    delete c; // derived class destructor is called, and then the base class's

    //cout<<"Example 4"<<endl;
    //MathNode_3* d = new MathNode_3; // type: Node_3* (Error due to abstract class can't be instantiated)

    cout<<"END."<<endl;
    return 0;
}


Output:

Example 1
This is constructor of Node.
This is constructor of MathNode.
This is destructor of MathNode.
This is destructor of Node.
Example 2
This is constructor of Node.
This is constructor of MathNode.
This is destructor of Node.
Example 3
This is constructor of Node_2.
This is constructor of MathNode_2.
This is destructor of MathNode_2.
This is destructor of Node_2.
END.


Summary:

1. "new" calls base class constructor, then derived class constructor; "delete" calls destructor in reverse order;

2. If use base class pointer to point to derived class instance, then the called non-virtual functions are from base class, while virtual functions (whose addresses are recorded in virtual function table vtable that is created by compiler on the construction of object. Each object has its own vtable) are from the object of derived class.

3. Pure virtual member function makes the class abstract. We can use it to prevent instantiating the base class (compiler error), while we can override the same function in derived class to make it concrete class.


Related articles:

1. C++虚函数浅析

2. pure Virtual Functions

3. C++函数调用内存分配机制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值