Runtime Flow blog (Understanding a control flow in a running .NET application)

July 1, 2010

Members initialization difference between C# and C++

Filed under: Uncategorized — Sergey Vlasov @ 5:36 pm

Joe Duffy presented an interesting example where order of members initialization is different between C# and C++ in his On partially-constructed objects blog post. “In C#, member initializers run from most derived first, to least derived. In C++, however, member initializers run alongside the ordinary construction process.” I’ve traced his examples in Runtime Flow:

C# initialization

C# initialization

C++ initialization

C++ initialization

You see how in C# E’s member is constructed even before the D object, while in C++ E’s member is constructed the last. It is interesting to note that while logically members are initialized before constructors, technically constructors start first and then immediately initialize members.

Advertisement

Blog at WordPress.com.

%d bloggers like this: