code analyzers

Virtual function calls in constructors and destructors

In different programming languages, the behavior of virtual functions differs when it comes to constructors and destructors. Incorrect use of virtual functions is a classic mistake. Developers often use virtual functions incorrectly. In this article, we discuss this classic mistake.

Virtual function calls in constructors and destructors

by Andrey Karpov

From the article:

So, what's the problem? You can find this information in any C++ programming book. The problem is that it's easy to forget about it! Thus, some programmers assume that foo and bar functions are called from the most derived C class. People keep asking the same question on forums: "Why does the code run in an unexpected way?" I think now you understand why it's easy to make a mistake in such code. Especially if you write code in other languages where the behavior is different. Let's look at the code fragment in C#.