QScopedValueRollback Class
template <typename T> class QScopedValueRollbackQScopedValueRollback 类会在变量销毁时将其重置为之前的值。更多
Header: | #include <QScopedValueRollback> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
公共函数
QScopedValueRollback(T &var) | |
QScopedValueRollback(T &var, T value) | |
~QScopedValueRollback() | |
void | commit() |
详细说明
QScopedValueRollback 类可用于在异常抛出时恢复状态,而无需编写 try-catch 块。
该类还可用于管理临时设置的变量,例如重入保护。使用该类后,无论函数是正常退出、通过返回语句提前退出还是通过异常退出,变量都将被重置。
该模板只能与支持赋值的类型实例化。
另请参阅 QScopedPointer 和QScopeGuard 。
成员函数文档
[explicit constexpr]
QScopedValueRollback::QScopedValueRollback(T &var)
在内部存储var 之前的值,以便销毁时还原。
[explicit constexpr]
QScopedValueRollback::QScopedValueRollback(T &var, T value)
将value 赋值给 var,并在内部存储var 之前的值,以便销毁时还原。
[constexpr]
QScopedValueRollback::~QScopedValueRollback()
将先前的值赋给托管变量。这是构建时的值,或最后一次调用commit() 时的值。
[constexpr]
void QScopedValueRollback::commit()
将受管变量之前的值更新为当前值。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.