Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Base {
- Base() { std::cout << "in Base ctor "; f(); }
- virtual void f() { std::cout << "Base::f() is called" << std::endl; }
- virtual ~Base() {}
- };
- struct Derived: public Base {
- Derived() : Base() { std::cout << "in Derived ctor "; f(); }
- virtual void f() { std::cout << "Derived::f() is called" << std::endl; }
- virtual ~Derived() {}
- };
- int main()
- {
- Base *d = new Derived; // during construction: there is no polymorphism
- /*
- * Prints:
- * in Base ctor Base::f() is called
- * in Derived ctor Derived::f() is called
- */
- d->f(); // during normal member function call: polymorphism works
- /* Prints:
- * Derived::f() is called
- */
- delete d;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement