Created
February 9, 2015 13:23
-
-
Save junfenglx/3900075dd3311e875e11 to your computer and use it in GitHub Desktop.
In class constructor and destructor call virtual functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Author
junfenglx
commented
Feb 9, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment