이 페이지에서

QWeakPointer Class

template <typename T> class QWeakPointer

QWeakPointer 클래스는 공유 포인터에 대한 약한 참조를 보유합니다. 더 보기...

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

참고: 이 클래스의 모든 함수는 재진입 가능합니다.

공개 함수

QWeakPointer()
QWeakPointer(const QSharedPointer<T> &other)
QWeakPointer(const QWeakPointer<T> &other)
~QWeakPointer()
void clear()
bool isNull() const
QSharedPointer<T> lock() const
(since 6.7) bool owner_before(const QSharedPointer<X> &other) const
(since 6.7) bool owner_before(const QWeakPointer<X> &other) const
(since 6.7) bool owner_equal(const QSharedPointer<X> &other) const
(since 6.7) bool owner_equal(const QWeakPointer<X> &other) const
(since 6.7) size_t owner_hash() const
void swap(QWeakPointer<T> &other)
QSharedPointer<T> toStrongRef() const
operator bool() const
bool operator!() const
QWeakPointer<T> &operator=(const QSharedPointer<T> &other)
QWeakPointer<T> &operator=(const QWeakPointer<T> &other)
QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &src)
bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2)
bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2)
bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t)
bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs)
bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2)
bool operator==(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2)
bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t)
bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs)

상세 설명

QWeakPointer는 C++에서 포인터에 대한 자동 약한 참조로, 템플릿 매개 변수 T 는 추적되는 포인터의 유형을 지정합니다. 이 객체를 사용하여 포인터의 참조를 직접 해제할 수는 없지만, 다른 컨텍스트에서 포인터가 삭제되었는지 여부를 확인하는 데 사용할 수 있습니다.

QWeakPointer 객체는 QSharedPointer 로부터의 할당을 통해서만 생성될 수 있습니다.

QWeakPointer는 실수를 방지하기 위해 자동 형 변환 연산자를 제공하지 않는다는 점에 유의해야 합니다. QWeakPointer가 포인터를 추적하더라도, 가리키는 객체가 유효한 상태를 유지한다고 보장하지 않으므로 QWeakPointer 자체를 포인터로 간주해서는 안 됩니다.

따라서 QWeakPointer가 추적하는 포인터에 접근하려면, 먼저 이를 QSharedPointer 로 승격시킨 후 결과 객체가 null인지 여부를 확인해야 합니다. QSharedPointer 는 객체가 삭제되지 않았음을 보장하므로, null이 아닌 객체를 얻었다면 해당 포인터를 사용할 수 있습니다. 예제는 QWeakPointer::toStrongRef()을 참조하십시오.

QSharedPointer 및 QScopedPointer도 참조하십시오 .

멤버 함수 문서

QWeakPointer::QWeakPointer()

아무것도 가리키지 않는 QWeakPointer를 생성합니다.

QWeakPointer::QWeakPointer(const QSharedPointer<T> &other)

other 가 가리키는 포인터에 대한 약한 참조를 보유하는 QWeakPointer를 생성합니다.

T 가 이 클래스의 템플릿 매개변수의 파생형인 경우, QWeakPointer는 자동 형변환을 수행합니다. 그렇지 않은 경우 컴파일 오류가 발생합니다.

[noexcept] QWeakPointer::QWeakPointer(const QWeakPointer<T> &other)

other 가 가리키는 포인터에 대한 약한 참조를 보유하는 QWeakPointer를 생성합니다.

T 가 이 클래스의 템플릿 매개변수의 파생형인 경우, QWeakPointer는 자동 형 변환을 수행합니다. 그렇지 않은 경우 컴파일러 오류가 발생합니다.

QWeakPointer::~QWeakPointer()

이 QWeakPointer 객체를 파기합니다. 이 객체가 참조하는 포인터는 삭제되지 않습니다.

void QWeakPointer::clear()

이 QWeakPointer 객체를 지우고, 해당 객체가 포인터에 대해 가지고 있을 수 있는 참조를 해제합니다.

bool QWeakPointer::isNull() const

이 객체가 ` nullptr`을 가리키는 경우 ` true `을 반환합니다.

약한 참조의 특성상, QWeakPointer 가 가리키는 포인터는 언제든지 nullptr 가 될 수 있으므로, 이 함수에서 반환되는 값은 호출할 때마다 false에서 true로 바뀔 수 있습니다.

QSharedPointer<T> QWeakPointer::lock() const

toStrongRef()와 동일합니다.

이 함수는 std::weak_ptr과의 API 호환성을 위해 제공됩니다.

[noexcept, since 6.7] template <typename X> bool QWeakPointer::owner_before(const QSharedPointer<X> &other) const

[noexcept, since 6.7] template <typename X> bool QWeakPointer::owner_before(const QWeakPointer<X> &other) const

이 스마트 포인터가 구현에 정의된 소유자 기반 정렬 순서에서 other 보다 앞서 있을 때만 true 를 반환합니다. 이 정렬 순서는 두 스마트 포인터가 모두 비어 있거나, 둘 다 동일한 객체를 소유하고 있는 경우(겉보기 유형과 포인터가 다르더라도) 서로 동등한 것으로 간주됩니다.

이 함수들은 Qt 6.7에서 도입되었습니다.

owner_equal도 참조하십시오 .

[noexcept, since 6.7] template <typename X> bool QWeakPointer::owner_equal(const QSharedPointer<X> &other) const

[noexcept, since 6.7] template <typename X> bool QWeakPointer::owner_equal(const QWeakPointer<X> &other) const

이 스마트 포인터와 ` other `가 소유권을 공유하는 경우에만 ` true `을 반환합니다.

이 함수들은 Qt 6.7에서 도입되었습니다.

owner_before 및 owner_hash도 참조하십시오 .

[noexcept, since 6.7] size_t QWeakPointer::owner_hash() const

이 스마트 포인터 객체에 대한 소유자 기반 해시 값을 반환합니다. ( owner_equal 에 따라) 동일하게 비교되는 스마트 포인터들은 동일한 소유자 기반 해시 값을 가집니다.

이 함수는 Qt 6.7에서 도입되었습니다.

owner_equal도 참조하십시오 .

[noexcept] void QWeakPointer::swap(QWeakPointer<T> &other)

이 약한 포인터 인스턴스를 other 로 교체합니다. 이 작업은 매우 빠르며 절대 실패하지 않습니다.

QSharedPointer<T> QWeakPointer::toStrongRef() const

이 약한 참조를 강한 참조로 승격시키고, 해당 참조를 포함하는 ` QSharedPointer ` 객체를 반환합니다. ` QSharedPointer`로 승격할 때, 이 함수는 해당 객체가 이미 삭제되었는지 여부를 확인합니다. 아직 삭제되지 않은 경우, 이 함수는 공유 객체에 대한 참조 횟수를 증가시켜 해당 객체가 삭제되지 않도록 보장합니다.

이 함수는 공유 객체에 대한 유효한 강한 참조를 얻지 못할 수도 있으므로, 반환된 객체에 대해 QSharedPointer::isNull()를 호출하여 변환이 성공했는지 항상 확인해야 합니다.

예를 들어, 다음 코드는 강 참조로 보유되었던 ` QWeakPointer `를 승격시키고, 성공한 경우 보유되었던 정수의 값을 출력합니다:

QWeakPointer<int> weakref;

// ...

QSharedPointer<int> strong = weakref.toStrongRef();
if (strong)
    qDebug() << "The value is:" << *strong;
else
    qDebug() << "The value has already been deleted";

QSharedPointer::QSharedPointer()도 참조하십시오 .

QWeakPointer::operator bool() const

포함된 포인터가 nullptr 가 아닌 경우 true 를 반환합니다. 이 함수는 다음과 같이 if-constructs 에서 사용하기에 적합합니다:

if (weakref) { /*...*/ }

약한 참조의 특성상, ` QWeakPointer `가 가리키는 포인터는 언제든지 ` nullptr `가 될 수 있으므로, 이 함수에서 반환되는 값은 호출할 때마다 `true`에서 `false`로 바뀔 수 있습니다.

isNull()도 참조하십시오 .

bool QWeakPointer::operator!() const

이 객체가 nullptr 를 가리키는 경우 true 를 반환합니다. 이 함수는 다음과 같이 if-constructs 에서 사용하기에 적합합니다:

if (!weakref) { /*...*/ }

약한 참조의 특성상, QWeakPointer 가 가리키는 포인터는 언제든지 nullptr 로 변경될 수 있으므로, 이 함수에서 반환되는 값은 호출할 때마다 false에서 true로 바뀔 수 있습니다.

isNull()도 참조하십시오 .

QWeakPointer<T> &QWeakPointer::operator=(const QSharedPointer<T> &other)

이 객체가 ` other`의 포인터를 공유하도록 합니다. 현재 포인터 참조는 폐기되지만 삭제되지는 않습니다.

T 가 이 클래스의 템플릿 매개변수의 파생형인 경우, ` QWeakPointer `는 자동 형 변환을 수행합니다. 그렇지 않은 경우 컴파일러 오류가 발생합니다.

[noexcept] QWeakPointer<T> &QWeakPointer::operator=(const QWeakPointer<T> &other)

이 객체가 ` other`의 포인터를 공유하도록 합니다. 현재 포인터 참조는 버려지지만 삭제되지는 않습니다.

T 가 이 클래스의 템플릿 매개변수의 파생형인 경우, ` QWeakPointer `은 자동 형 변환을 수행합니다. 그렇지 않은 경우 컴파일러 오류가 발생합니다.

관련 비멤버

template <typename X, typename T> QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &src)

src 가 보유한 포인터에 대한 약한 포인터를, X 유형으로 형변환하여 반환합니다. static_cast 가 성공하려면 T 및 X 유형이 하나의 계층 구조에 속해야 합니다.

X 는 T 와 동일한 cv-qualifiers(const 및 volatile)를 가져야 하며, 그렇지 않으면 코드가 컴파일되지 않습니다. const 속성을 제거하려면 qSharedPointerConstCast 를 사용하십시오.

template <typename T, typename X> bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2)

ptr1 와 ptr2 가 서로 다른 포인터를 가리키는 경우 true 를 반환합니다.

ptr2 의 템플릿 매개변수가 ptr1 의 것과 다른 경우, QSharedPointer 는 비교 대상 포인터가 동일하도록 보장하기 위해 자동 static_cast 를 시도합니다. ptr2 의 템플릿 매개변수가 ptr1 의 기저형이나 파생형이 아닌 경우, 컴파일러 오류가 발생합니다.

template <typename T, typename X> bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2)

ptr1 와 ptr2 가 서로 다른 포인터를 가리킬 경우 true 를 반환합니다.

ptr2 의 템플릿 매개변수가 ptr1 의 것과 다른 경우, QSharedPointer 는 비교 대상 포인터들이 동일하도록 보장하기 위해 자동 static_cast 를 시도합니다. ptr2 의 템플릿 매개변수가 ptr1 의 기저형이나 파생형이 아닌 경우, 컴파일러 오류가 발생합니다.

template <typename T> bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t)

lhs 가 유효한(즉, null이 아닌) 포인터를 가리키는 경우 true 를 반환합니다.

QWeakPointer::isNull()도 참조하십시오 .

template <typename T> bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs)

rhs 가 유효한(즉, null이 아닌) 포인터를 가리키는 경우 true 를 반환합니다.

QWeakPointer::isNull()도 참조하십시오 .

template <typename T, typename X> bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2)

ptr1 와 ptr2 가 동일한 포인터를 가리키는 경우 true 를 반환합니다.

ptr2 의 템플릿 매개변수가 ptr1 의 것과 다른 경우, QSharedPointer 는 비교 대상 포인터가 동일하도록 보장하기 위해 자동 static_cast 을 시도합니다. ptr2 의 템플릿 매개변수가 ptr1 의 기저형이나 파생형이 아닌 경우, 컴파일러 오류가 발생합니다.

template <typename T, typename X> bool operator==(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2)

ptr1 와 ptr2 가 동일한 포인터를 가리키는 경우 true 를 반환합니다.

ptr2 의 템플릿 매개변수가 ptr1 의 것과 다른 경우, QSharedPointer 는 비교 대상 포인터가 동일하도록 보장하기 위해 자동 static_cast 을 시도합니다. ptr2 의 템플릿 매개변수가 ptr1 의 기저형이나 파생형이 아닌 경우, 컴파일 오류가 발생합니다.

template <typename T> bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t)

lhs 가 nullptr 를 가리키는 경우 true 를 반환합니다.

QWeakPointer::isNull()도 참조하십시오 .

template <typename T> bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs)

rhs 가 nullptr 를 가리키는 경우 true 를 반환합니다.

QWeakPointer::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.