Is the destructor called if the constructor throws an exception?

前端 未结 8 969
不知归路
不知归路 2020-11-29 06:57

Looking for an answer for C# and C++. (in C#, replace \'destructor\' with \'finalizer\')

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 07:53

    The destructor of the class still being constructed is not called, because the object was never fully constructed.

    However, the destructor of its base class (if any) IS called, because the object was constructed as far as being a base class object.

    Moreover, any member variables will have their destructors called too (as others have noted).

    NB: this applies to C++

提交回复
热议问题