QScopedValueRollback 类

template <typename T> class QScopedValueRollback

QScopedValueRollback 类当销毁时将变量重置为其先前值。 更多...

头: #include <QScopedValueRollback>
CMake: find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共函数

  QScopedValueRollback (T & var , T value )
  QScopedValueRollback (T & var )
  ~QScopedValueRollback ()
void commit ()

详细描述

The QScopedValueRollback class can be used to revert state when an exception is thrown without needing to write try-catch blocks.

It can also be used to manage variables that are temporarily set, such as reentrancy guards. By using this class, the variable will be reset whether the function is exited normally, exited early by a return statement, or exited by an exception.

The template can only be instantiated with a type that supports assignment.

另请参阅 QScopedPointer and QScopeGuard .

成员函数文档编制

[since 5.4] QScopedValueRollback:: QScopedValueRollback ( T & var , T value )

赋值 value to var and stores the previous value of var internally, for revert on destruction.

该函数在 Qt 5.4 引入。

QScopedValueRollback:: QScopedValueRollback ( T & var )

Stores the previous value of var internally, for revert on destruction.

QScopedValueRollback:: ~QScopedValueRollback ()

Assigns the previous value to the managed variable. This is the value at construction time, or at the last call to commit ()

void QScopedValueRollback:: commit ()

Updates the previous value of the managed variable to its current value.