Thursday, November 04, 2004

Moving from C++ to C# or Managed C++?

If you should ever move from C++ to C# or Managed C++, then you will find this very interesting:

In C++, if you call a virtual function from a constructor or destructor, the compiler calls the instance of the virtual function defined for the class being constructed (for example, Base::SomeVirtFn if called from Base::Base), not the most derived instance.
...
[Things] are different in C#. Managed objects—whether in C#, managed C++, or any other .NET-compliant language—are created as their final type, which means that if you call a virtual function from a constructor or destructor, the system calls the most derived function.

But hey, you shouldn't call virtual functions from ctors/dtors anyway...

http://msdn.microsoft.com/msdnmag/issues/04/11/CQA/default.aspx
and
http://blogs.msdn.com/oldnewthing/archive/2004/04/28/122037.aspx

No comments: