C++ Class
2019.11.29 20:46

QScopedPointer 소개 및 사용법

조회 수 8029 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

Qt에서 제공하는 메모리 할당 및 해제를 도와주는 포인터에 대해서 간단히 알아보자.

 

QScopedPointer 는 범위를 벗어나면 자동으로 가리키는 객체를 삭제한다.

void foo()
{
    QScopedPointer<int> i(new int(42));
    ...
    if (someCondition)
        return; // int는 여기서 삭제된다.

} // 아니면 여기서 ...

 

함수의 종료 조건은 할당한 메모리를 누설하지 않는다.

 

그렇다면 포인터가 가리키는 객체에 어떻게 접근 할 수 있을까? QScopedPointer는 operator *operator-> 연사자가 구현되어있으므로 일반적인 포인터처럼 액세스 할 수 있다.

QScopedPointer<int> i(new int(42));
*i = 43;

 

할당 연산자 및 일부 연산자는 의도적으로 누락되었다. 대신 reset() 을 통해 포인터에 주소를 설정할 수 있다.

QScopedPointer<int> i(new int(42));
i = new int(43); // 컴파일되지 않음.
i.reset(new int(43)); // 문제없음. 가리키는 기존 개체(있는 경우)를 삭제하고 해당 포인터를 새로운 개체로 설정.

 

의도적으로 누락 된 또 다른 연산자는 포인터에 직접 액세스 할 수있는 연산자 T *() 이다.

int *foo()
{
    QScopedPointer<int> i(new int(42));
    ...
    return i; // 컴파일되지 않을 것이다.
}

범위가 지정된 포인터가 범위를 벗어나므로 반환하는 순간 객체가 삭제된다. 

대신 QScopedPointer에게 작업이 완료되었고 힙 객체의 소유권을 가진다는 것을 take()를 호출하여 알릴 수 있다.

int *foo()
{
    QScopedPointer<int> i(new int(42));
    ...
    if (someError)
        return 0; // 여기에서 삭제된다.
    return i.take(); // 힙에 객체가 남아 있음.
}

 

new [] 연산자 또는 malloc() 으로 할당 하는경우는 어떤지보자. 이러한 경우 해제를 정의하는 두 번째 템플릿 매개 변수를 이용한다.

QScopedPointer<int, QScopedPointerPodDeleter> pod(static_cast<int *>(malloc(sizeof int)));

QScopedPointer가 범위를 벗어난 경우 QScopedPointerPodDeleter 에서 free 된다.

 

delete [] 연산자 처럼 가리키는 객체를 삭제하는 QScopedArrayPointer 도 있다. 

void foo()
{
    QScopedArrayPointer<int> i(new int[10]);
    i[2] = 42;
    ...
    return; // 여기서 정수 배열은 삭제된다.
}

 


  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views436466
    read more
  2. Qt의 오픈소스 라이센스 소개

    Date2019.12.15 CategoryGeneral and Desktop Bymakersweb Views21857
    Read More
  3. No Image

    Qt for MCU 1.0 릴리즈

    Date2019.12.10 CategoryMobile and Embedded Bymakersweb Views15443
    Read More
  4. No Image

    Qt Marketplace 발표

    Date2019.12.02 CategoryGeneral and Desktop Bymakersweb Views8648
    Read More
  5. No Image

    QScopedPointer 소개 및 사용법

    Date2019.11.29 CategoryC++ Class Bymakersweb Views8029
    Read More
  6. QOpenVirtualkeyboard(Qt 5용 한글 및 영문, 숫자 가상키보드)

    Date2019.11.27 CategoryGeneral and Desktop Bymakersweb Views7760
    Read More
  7. Qt3D의 QML 타입으로 3D렌더링

    Date2019.11.20 CategoryQML and Qt Quick Bymakersweb Views9344
    Read More
  8. No Image

    라즈베리파이3에서 Boot to Qt 실행해보기

    Date2019.11.13 CategoryMobile and Embedded Bymakersweb Views9713
    Read More
  9. No Image

    Qt애플리케이션 객체(QCoreApplication, QGuiApplication, QApplication) 에 대해서

    Date2019.11.11 CategoryC++ Class Bymakersweb Views23958
    Read More
  10. No Image

    Qt Quick 3D 소개

    Date2019.11.09 CategoryQML and Qt Quick Bymakersweb Views12390
    Read More
  11. QPushButton 의 커스텀 이미지버튼

    Date2019.11.05 CategoryC++ Class Bymakersweb Views15543
    Read More
  12. qbs 사용 방법(Helloworld)

    Date2019.10.23 CategoryInstallation and Deployment Bymakersweb Views8584
    Read More
  13. 웹기반 Qt Design Viewer

    Date2019.10.23 CategoryQML and Qt Quick Bymakersweb Views11731
    Read More
  14. Qt Creator에서 Qt의존성 라이브러리 자동복사하기

    Date2019.10.19 CategoryInstallation and Deployment Bymakersweb Views12801
    Read More
  15. No Image

    Qt for Embedded Linux 화면출력

    Date2019.10.17 CategoryMobile and Embedded Bymakersweb Views11515
    Read More
  16. Windows에서 Qt 설치 따라하기

    Date2019.10.14 CategoryInstallation and Deployment Bymakersweb Views42665
    Read More
  17. Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들

    Date2019.10.13 CategoryInstallation and Deployment Bymakersweb Views9974
    Read More
  18. No Image

    많은 리소스를 사용하는 Qt프로젝트에서 고려해봐야 할 qmake 옵션

    Date2019.10.11 CategoryGeneral and Desktop Bymakersweb Views10078
    Read More
  19. No Image

    Qbs에 대한 소개와 설치하는 방법

    Date2019.10.09 CategoryInstallation and Deployment Bymakersweb Views9026
    Read More
  20. OpenGL 렌더링을 QtQuick과 통합하는 방법

    Date2019.10.01 CategoryQML and Qt Quick Bymakersweb Views8964
    Read More
  21. No Image

    QML내에서의 시그널, 슬롯 시스템

    Date2019.09.29 CategoryQML and Qt Quick Bymakersweb Views12878
    Read More
Board Pagination Prev 1 3 4 5 6 7 9 Next
/ 9