QScopedPointer Class
template <typename T, typename Cleanup = QScopedPointerDeleter<T>> class QScopedPointerQScopedPointer クラスは、動的に割り当てられたオブジェクトへのポインタを格納し、破棄時にそれを削除します。詳細...
ヘッダー | #include <QScopedPointer> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
継承元: |
注意:このクラスの関数はすべてリエントラントです。
パブリック関数
QScopedPointer(T *p = nullptr) | |
~QScopedPointer() | |
T * | data() const |
T * | get() const |
bool | isNull() const |
void | reset(T *other = nullptr) |
bool | operator bool() const |
bool | operator!() const |
T & | operator*() const |
T * | operator->() const |
関連する非メンバー
bool | operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) |
bool | operator!=(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) |
bool | operator!=(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) |
bool | operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) |
bool | operator==(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) |
bool | operator==(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) |
詳細説明
ヒープに割り当てられたオブジェクトを手動で管理するのは大変で、エラーが発生しやすい。QScopedPointerは、ヒープ割り当てにスタックベースのメモリ所有権を割り当てることで、これを大幅に簡略化する小さなユーティリティ・クラスで、より一般的にはRAII(resource acquisition is initialization)と呼ばれます。
QScopedPointerは、現在のスコープがなくなったときに、指されたオブジェクトが削除されることを保証する。
ヒープ割り当てを行い、さまざまな終了点を持つこの関数を考えてみよう:
void myFunction(bool useSubClass) { MyClass *p = useSubClass ? new MyClass() : new MySubClass; QIODevice *device = handsOverOwnership(); if (m_value > 3) { delete p; delete device; return; } try { process(device); } catch (...) { delete p; delete device; throw; } delete p; delete device; }
この関数はヒープ割り当てを行い、さまざまな終了点を持つ。QScopedPointerを使えば、コードは次のように単純化できる:
void myFunction(bool useSubClass) { // assuming that MyClass has a virtual destructor QScopedPointer<MyClass> p(useSubClass ? new MyClass() : new MySubClass); QScopedPointer<QIODevice> device(handsOverOwnership()); if (m_value > 3) return; process(device); }
コンパイラがQScopedPointer用に生成するコードは、手動で記述する場合と同じです。delete を使用するコードは、QScopedPointer を使用する候補です(使用しない場合は、QSharedPointer などの別のタイプのスマート・ポインタを使用することもできます)。QScopedPointerは、所有権と寿命が明確に伝達されるように、意図的にコピーコンストラクタや代入演算子を持ちません。
通常の C++ ポインタの const 修飾も QScopedPointer で表現できます:
const QWidget *const p = new QWidget(); // is equivalent to: const QScopedPointer<const QWidget> p(new QWidget()); QWidget *const p = new QWidget(); // is equivalent to: const QScopedPointer<QWidget> p(new QWidget()); const QWidget *p = new QWidget(); // is equivalent to: QScopedPointer<const QWidget> p(new QWidget());
カスタム・クリーンアップ・ハンドラ
配列や、malloc
を使用して割り当てられたポインタは、delete
を使用して削除してはなりません。QScopedPointerの2番目のテンプレート・パラメータは、カスタム・クリーンアップ・ハンドラに使用できます。
以下のカスタム・クリーンアップ・ハンドラがあります:
- QScopedPointerDeleter - デフォルト。
delete
- QScopedPointerArrayDeleter -
delete []
を使用してポインタを削除します。このハンドラは、new []
で割り当てられたポインタに使用します。 - QScopedPointerPodDeleter -
free()
を使用してポインタを削除します。malloc()
で割り当てられたポインタには、このハンドラを使用してください。 - QScopedPointerDeleteLater - ポインタに対して
deleteLater()
を呼び出してポインタを削除します。このハンドラは、QEventLoop にアクティブに参加しているQObject へのポインタに使用します。
独自のクラスをハンドラとして渡すこともできます。ただし、そのクラスがパブリックな静的関数void cleanup(T *pointer)
を持っていることが条件です。
// this QScopedPointer deletes its data using the delete[] operator: QScopedPointer<int, QScopedPointerArrayDeleter<int> > arrayPointer(new int[42]); // this QScopedPointer frees its data using free(): QScopedPointer<int, QScopedPointerPodDeleter> podPointer(reinterpret_cast<int *>(malloc(42))); // this struct calls "myCustomDeallocator" to delete the pointer struct ScopedPointerCustomDeleter { static inline void cleanup(MyCustomClass *pointer) { myCustomDeallocator(pointer); } }; // QScopedPointer using a custom deleter: QScopedPointer<MyCustomClass, ScopedPointerCustomDeleter> customPointer(new MyCustomClass);
前方宣言ポインタ
前方宣言されたクラスは、QScopedPointerのクリーンアップが必要なときに前方宣言されたクラスのデストラクタが利用可能である限り、QScopedPointer内で使用することができます。
具体的には、前方宣言されたクラスを指す QScopedPointer を含むすべてのクラスは、非インラインのコンストラクタ、デストラクタ、および代入演算子を持たなければなりません:
class MyPrivateClass; // forward declare MyPrivateClass class MyClass { private: QScopedPointer<MyPrivateClass> privatePtr; // QScopedPointer to forward declared class public: MyClass(); // OK inline ~MyClass() {} // VIOLATION - Destructor must not be inline private: Q_DISABLE_COPY(MyClass) // OK - copy constructor and assignment operators // are now disabled, so the compiler won't implicitly // generate them. };
そうでない場合、コンパイラーはMyPrivateClass
をデストラクトできないという警告を出力します。
QSharedPointerも参照してください 。
メンバ関数のドキュメント
[explicit noexcept]
QScopedPointer::QScopedPointer(T *p = nullptr)
この QScopedPointer インスタンスを構築し、そのポインタをp に設定します。
QScopedPointer::~QScopedPointer()
このQScopedPointer オブジェクトを破棄する。ポインタが指すオブジェクトを削除します。
[noexcept]
T *QScopedPointer::data() const
このオブジェクトが参照しているポインタの値を返す。QScopedPointer は、指されたオブジェクトをまだ所有している。
[noexcept]
T *QScopedPointer::get() const
data() と同じ。
[noexcept]
bool QScopedPointer::isNull() const
このオブジェクトがnullptr
を参照している場合はtrue
を返す。
[noexcept(...)]
void QScopedPointer::reset(T *other = nullptr)
other QScopedPointer は を所有することになり、そのデストラクタで削除される。other
注: noexcept(Cleanup::cleanup(std::declval<T *>()))
がtrue
のとき、この関数は noexcept となる。
[explicit]
bool QScopedPointer::operator bool() const
含まれるポインタがnullptr
でない場合はtrue
を返す。この関数はif-constructs
のような用途に適している:
if (scopedPointer) { ... }
isNull()も参照 。
[noexcept]
bool QScopedPointer::operator!() const
このオブジェクトがnullptr
を参照している場合はtrue
を返す。
isNull()も参照 。
T &QScopedPointer::operator*() const
スコープされたポインタのオブジェクトへのアクセスを提供する。
含まれるポインタがnullptr
の場合、動作は未定義です。
isNull()も参照 。
[noexcept]
T *QScopedPointer::operator->() const
スコープされたポインタのオブジェクトへのアクセスを提供する。
含まれるポインタがnullptr
の場合、動作は未定義です。
isNull()も参照 。
関連する非メンバー
[noexcept]
bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
lhs とrhs が異なるポインタを指している場合はtrue
を返す。
[noexcept]
bool operator!=(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t)
lhs が有効な(すなわち NULL ではない)ポインタを指している場合はtrue
を返す。
QScopedPointer::isNull()も参照のこと 。
[noexcept]
bool operator!=(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs)
rhs が有効な(すなわち NULL ではない)ポインタを指している場合はtrue
を返す。
QScopedPointer::isNull()も参照のこと 。
[noexcept]
bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
lhs とrhs が同じポインタを指している場合、true
を返す。
[noexcept]
bool operator==(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t)
lhs がnullptr
を参照している場合はtrue
を返す。
QScopedPointer::isNull()も参照のこと 。
[noexcept]
bool operator==(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs)
rhs がnullptr
を参照している場合はtrue
を返す。
QScopedPointer::isNull()も参照のこと 。
© 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.