このページでは

QScopedPointer Class

template <typename T, typename Cleanup> class QScopedPointer

QScopedPointer クラスは、動的に割り当てられたオブジェクトへのポインタを格納し、破棄時にそのオブジェクトを削除します。詳細...

ヘッダー: #include <QScopedPointer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
継承元:

QScopedArrayPointer

注:このクラスのすべての関数は再入可能です。

パブリック関数

QScopedPointer(T *p = nullptr)
~QScopedPointer()
T *data() const
T *get() const
bool isNull() const
void reset(T *other = nullptr)
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)」と呼ばれています。テンプレートパラメータT は、管理対象のポインタの型を指定します。

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;
}

このコードは、手動での `delete` 呼び出しによって煩雑になっています。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.data());
}

コンパイラが QScopedPointer 用に生成するコードは、手動で記述した場合と同じです。delete を利用するコードは、QScopedPointer を使用する候補となります(そうでない場合は、QSharedPointer などの別の種類のスマートポインタが適している可能性があります)。QScopedPointer には、所有権と存続期間を明確に伝えるため、意図的にコピーコンストラクタや代入演算子が用意されていません。

通常の C++ ポインタに対する const 修飾も、QScopedPointer を使用して表現できます:

    const QWidget *const p = new QWidget();
    // is equivalent to:
    const QScopedPointer<const QWidget> p1(new QWidget());

    QWidget *const p = new QWidget();
    // is equivalent to:
    const QScopedPointer<QWidget> p1(new QWidget());

    const QWidget *p = new QWidget();
    // is equivalent to:
    QScopedPointer<const QWidget> p1(new QWidget());

カスタムクリーンアップハンドラ

malloc で割り当てられたポインタおよび配列は、delete を使用して削除してはなりません。Cleanup テンプレートパラメータは、スコープ付きポインタが破棄される際に使用するクリーンアップハンドラを指定します。デフォルトでは QScopedPointerDeleter が使用されます。

以下のカスタムクリーンアップハンドラが存在します:

  • 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] 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()も参照してください 。

© 2026 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.