A
Alex Vinokur
Microsoft C++ Version 13.10.3077
Why is virtual function init() called in constructor here?
====== foo.cpp ======
#include "iostream"
using namespace std;
class Base
{
public:
virtual void init () {cout << "Base::init()" << endl; }
Base() { cout << "Base::Ctor()" << endl;}
};
class Derived : public Base
{
public:
void foo() { cout << "Derived::foo()" << endl; }
virtual void init () { cout << "Derived::init()" << endl; foo ();
}
Derived () : Base() { cout << "Derived::Ctor()" << endl; init ();}
};
int main ()
{
Base * pb = new Derived;
return 0;
}
=====================
====== Run ======
Base::Ctor()
Derived::Ctor()
Derived::init() // Why not Base::init()
Derived::foo()
================
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Why is virtual function init() called in constructor here?
====== foo.cpp ======
#include "iostream"
using namespace std;
class Base
{
public:
virtual void init () {cout << "Base::init()" << endl; }
Base() { cout << "Base::Ctor()" << endl;}
};
class Derived : public Base
{
public:
void foo() { cout << "Derived::foo()" << endl; }
virtual void init () { cout << "Derived::init()" << endl; foo ();
}
Derived () : Base() { cout << "Derived::Ctor()" << endl; init ();}
};
int main ()
{
Base * pb = new Derived;
return 0;
}
=====================
====== Run ======
Base::Ctor()
Derived::Ctor()
Derived::init() // Why not Base::init()
Derived::foo()
================
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn