Google Interview Question
Software Engineer / DevelopersThe answer he is looking for is that you shouldn't call virtual functions from a constructor because the vtable has not yet been setup at instantiation. So it will call one of the base classes function I believe.
It is usually safe to call any member function from within a constructor because the object has been completely set up (virtual tables have been initialized and so on) prior to the execution of the first line of user code. However, it is potentially unsafe for a member function to call a virtual member function for an abstract base class during construction or destruction.
Constructors can call virtual functions. When virtual functions are called, the function invoked is the function defined for the constructor's own class (or inherited from its bases). The following example shows what happens when a virtual function is called from within a constructor:
// specl_calling_virtual_functions.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
class Base
{
public:
Base(); // Default constructor.
virtual void f(); // Virtual member function.
};
Base::Base()
{
cout << "Constructing Base sub-object\n";
f(); // Call virtual member function
} // from inside constructor.
void Base::f()
{
cout << "Called Base::f()\n";
}
class Derived : public Base
{
public:
Derived(); // Default constructor.
void f(); // Implementation of virtual
}; // function f for this class.
Derived::Derived()
{
cout << "Constructing Derived object\n";
}
void Derived::f()
{
cout << "Called Derived::f()\n";
}
int main()
{
Derived d;
}
When the preceding program is run, the declaration Derived d causes the following sequence of events:
The constructor for class Derived (Derived::Derived) is called.
Prior to entering the body of the Derived class's constructor, the constructor for class Base (Base::Base) is called.
Base::Base calls the function f, which is a virtual function. Ordinarily, Derived::f would be called because the object d is of type Derived. Because the Base::Base function is a constructor, the object is not yet of the Derived type, and Base::f is called.
If it's a pure virtual function, you can't create an instance of that object.
- loky November 12, 2008