Exception Safety in Qt

前端 未结 4 2044
Happy的楠姐
Happy的楠姐 2020-12-30 02:44

Wikipedia says that \"A piece of code is said to be exception-safe, if run-time failures within the code will not produce ill effects, such as memory leaks, garbled stored d

4条回答
  •  粉色の甜心
    2020-12-30 03:26

    C++ has a very powerful mechanism for excpetion-safety. Destructors are run for all variables that go out of scope due to an exception. This differs from languages like Java, where exception-safety requires the programmer to get the catch and finally clauses right.

    The C++ behavior of calling destructors works seamlessly with Qt objects on the stack. Qt classes all have destructors and none require manual cleanup. Furthermore, QSharedPointer can be used to manage heap-allocated Qt objects; when the last pointer goes out of scope the object is destroyed. This includes the case where the pointer goes out of scope due to an exception.

    So, exception-safety is certainly present in Qt. It's just transparent.

提交回复
热议问题