summaryrefslogtreecommitdiffstats
path: root/virtual_destructor.cpp
blob: 75324c6e813c17eaa4073db4fd39dc5e358d1472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* outputs:
 *
 * derived destructor called
 * base destructor called
 */

#include <iostream>

/* will generate a class's default ctor, copy ctor, copy assignment operator,
 * by default data member and dtor are private, default ctor, copy ctor and
 * assignment operator are public
 */
class Base
{
  public:
    virtual ~Base();
};
Base::~Base() {
    std::cout << "base destructor called\n";
}

class Derived : public Base
{
  public:
    ~Derived();
};
Derived::~Derived() {
    std::cout << "derived destructor called\n";
}

int main(int argc, char **argv)
{
    /* when d goes out of scope, it will call its destructor will call base
     * class destructor
     */
    Derived d;
    Base b = d; /* conversion of d into Base takes place on rhs, d is sliced down */

    return 0;
}