QWeakPointer Class
template <typename T> class QWeakPointerQWeakPointer 클래스는 공유 포인터에 대한 약한 참조를 보유합니다. 더 보기...
Header: | #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 |
bool | 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++에서 포인터에 대한 자동 약한 참조입니다. 포인터를 직접 역참조하는 데는 사용할 수 없지만, 다른 컨텍스트에서 포인터가 삭제되었는지 여부를 확인하는 데는 사용할 수 있습니다.
QWeakPointer 객체는 QSharedPointer 에서 할당을 통해서만 생성할 수 있습니다.
한 가지 주의할 점은 QWeakPointer는 실수를 방지하기 위해 자동 캐스팅 연산자를 제공하지 않는다는 것입니다. QWeakPointer가 포인터를 추적하더라도 포인팅된 객체가 유효하다는 것을 보장하지 않으므로 포인터 자체를 포인터로 간주해서는 안 됩니다.
따라서 QWeakPointer가 추적 중인 포인터에 액세스하려면 먼저 QSharedPointer 로 승격하고 결과 객체가 null인지 여부를 확인해야 합니다. QSharedPointer 은 객체가 삭제되지 않았음을 보장하므로 null이 아닌 객체를 얻으면 포인터를 사용할 수 있습니다. 예는 QWeakPointer::toStrongRef()를 참조하세요.
QSharedPointer 및 QScopedPointer 을참조하세요 .
멤버 함수 문서
[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 를참조하십시오 .
QWeakPointer::QWeakPointer()
아무것도 가리키지 않는 QWeak포인터를 생성합니다.
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
이 될 수 있으므로 이 함수에서 반환되는 값은 호출할 때마다 거짓에서 참으로 바뀔 수 있다는 점에 유의하세요.
QSharedPointer<T> QWeakPointer::lock() const
toStrongRef()와 동일합니다.
이 함수는 std::weak_ptr과의 API 호환성을 위해 제공됩니다.
[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()도 참조하세요 .
bool QWeakPointer::operator bool() const
포함된 포인터가 nullptr
이 아닌 경우 true
을 반환합니다. 이 함수는 if-constructs
와 같이 사용하기에 적합합니다:
if (weakref) { ... }
약한 참조의 특성상 QWeakPointer 이 참조하는 포인터는 언제든지 nullptr
이 될 수 있으므로 이 함수에서 반환되는 값은 호출할 때마다 참에서 거짓으로 바뀔 수 있습니다.
isNull()도 참조하세요 .
bool QWeakPointer::operator!() const
이 객체가 nullptr
을 참조하는 경우 true
을 반환합니다. 이 함수는 if-constructs
과 같이 사용하기에 적합합니다:
if (!weakref) { ... }
약한 참조의 특성상 QWeakPointer 이 참조하는 포인터는 언제든지 nullptr
이 될 수 있으므로 이 함수에서 반환되는 값은 호출할 때마다 거짓에서 참으로 바뀔 수 있습니다.
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
유형으로 캐스팅합니다. T
및 X
타입은 static_cast
이 성공하려면 하나의 계층 구조에 속해야 합니다.
X
에는 T
과 동일한 cv 한정자(const
및 volatile
)가 있어야 하며, 그렇지 않으면 코드가 컴파일되지 않습니다. qSharedPointerConstCast 을 사용하여 constness를 제거하세요.
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 이 유효한(즉, 널이 아닌) 포인터를 참조하는 경우 true
을 반환합니다.
QWeakPointer::isNull()도 참조하세요 .
template <typename T> bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs)
rhs 이 유효한(즉, 널이 아닌) 포인터를 참조하는 경우 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()도 참조하세요 .
© 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.