Skip to content

Instantly share code, notes, and snippets.

@junfenglx
Created February 9, 2015 13:23

Revisions

  1. junfeng_hu created this gist Feb 9, 2015.
    41 changes: 41 additions & 0 deletions test_virtual_func.cc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #include <iostream>

    class Base {
    public:
    Base() {
    std::cout << "Base Constructor" << std::endl;
    ctor();
    }
    virtual ~Base() {
    std::cout << "Base Destructor" << std::endl;
    dtor();
    }
    virtual void ctor() {
    std::cout << "in Base::ctor" << std::endl;
    }
    virtual void dtor() {
    std::cout << "in Base::dtor" << std::endl;
    }
    };
    class Derived: public Base {
    public:
    Derived() {
    std::cout << "Derived Constructor" << std::endl;
    ctor();
    }
    ~Derived() {
    std::cout << "Derived Destructor" << std::endl;
    dtor();
    }
    virtual void ctor() {
    std::cout << "in Derived::ctor" << std::endl;
    }
    virtual void dtor() {
    std::cout << "in Derived::dtor" << std::endl;
    }
    };

    int main() {
    Base *pb = new Derived();
    delete pb;
    }