Calling virtual methods in constructors.
So I got given this warning today and went hunting for an explanation. It's an interesting point so I thought I'd use it to mark a return to some blogging.
I got my information from these two links: Meyer Effective C++ and a discussion on it's implications in C#.
You have class A and B.
B derives A.
B implements some virtual method foo();
A's constructor calls foo();
C++:
In C++ when you're calling constructors, at the time you call the base constructor your object instance is not yet of type B and so A's implementation of foo() is called. This is safe and good.
Why? Well consider what happens when B's implementation of foo() depends on some setup on B's ctor.
C# and Java:
These two recklessly call the lowest implementation in the polymorphic chain. Again consider what happens when B uses some custom setup in B::ctor(). Kablamo.
So there you go.

