QEnableSharedFromThis Class
template <typename T> class QEnableSharedFromThis이미 공유 포인터로 관리되는 객체에 대해 QSharedPointer 를 가져올 수 있는 베이스 클래스입니다. 더 보기...
헤더: | #include <QEnableSharedFromThis> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
공용 함수
QSharedPointer<T> | sharedFromThis() |
QSharedPointer<const T> | sharedFromThis() const |
자세한 설명
클래스의 인스턴스에서 QSharedPointer 를 만들어야 할 때, 예를 들어 객체 자체에서 이 클래스를 상속할 수 있습니다. 중요한 점은 QSharedPointer<T>(this)를 반환하는 기법을 사용할 수 없다는 것입니다. 이렇게 하면 별도의 참조 수를 가진 별개의 QSharedPointer 객체가 여러 개 생성되기 때문입니다. 따라서 동일한 원시 포인터에서 QSharedPointer 를 두 개 이상 생성해서는 안 됩니다.
QEnableSharedFromThis는 sharedFromThis()라는 두 개의 멤버 함수를 정의하여 QSharedPointer<T>와 QSharedPointer<const T>를 constness에 따라 this
으로 반환합니다:
class Y: public QEnableSharedFromThis<Y> { public: QSharedPointer<Y> f() { return sharedFromThis(); } }; int main() { QSharedPointer<Y> p(new Y()); QSharedPointer<Y> y = p->f(); Q_ASSERT(p == y); // p and q must share ownership }
클래스 자체 외부의 객체에서 공유 포인터를 가져올 수도 있습니다. 이는 현재 공유 포인터를 사용할 수 없는 스크립트에 인터페이스를 제공하는 코드에서 특히 유용합니다. 예를 들어
class ScriptInterface : public QObject { Q_OBJECT // ... public slots: void slotCalledByScript(Y *managedBySharedPointer) { QSharedPointer<Y> yPtr = managedBySharedPointer->sharedFromThis(); // Some other code unrelated to scripts that expects a QSharedPointer<Y> ... } };
멤버 함수 문서
QSharedPointer<T> QEnableSharedFromThis::sharedFromThis()
this
(즉, 이 메서드를 호출하는 서브클래스 인스턴스)가 QSharedPointer 에 의해 관리되고 있는 경우 this
를 가리키는 공유 포인터 인스턴스를 반환하고, 그렇지 않으면 null QSharedPointer 을 반환합니다.
QSharedPointer<const T> QEnableSharedFromThis::sharedFromThis() const
이것은 오버로드된 함수입니다.
sharedFromThis()의 Const 오버로드입니다.
© 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.