Qt_IM_3 - 8BitsCoding/RobotMentor GitHub Wiki


QScopedPointerλ₯Ό μ•„λž˜μ™€ 같이 μ‚¬μš©ν•  수 μžˆλ‹€.

C++11의 슀마트 포인터와 같은 역할이넀..

// main.cpp
#include <QtCore/QCoreApplication>

// The QScopedPointer class stores a pointer to a dynamically allocated object,
// and deletes it upon destruction!!!

#include <qdebug.h>
#include <qscopedpointer.h>

#include "test.h"

void useit(test* obj) {
	if (!obj) return;

	qInfo() << "Using" << obj;
}

void doStuff() {
	//test* t = new test();			// dangling(λ‹¬λž‘λ‹¬λž‘) pointer!!!
	QScopedPointer<test> mypointer(new test());

	//μ΄λ ‡κ²Œ 포인터λ₯Ό 넣을 수 μžˆλ‹€.
	useit(mypointer.data());		

	// pointer is automatically deleted!!
}

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	for (int i = 0; i < 100; i++) {
		doStuff();
	}

	return a.exec();
}
// test.h
#pragma once

#include <qobject.h>
#include <qdebug.h>

class test : public QObject
{
	Q_OBJECT
public:
	explicit test(QObject* parent = nullptr);
	~test();

signals:

public slots:
};
// test.cpp
#include "test.h"

#include <qdebug.h>

test::test(QObject* parent) : QObject(parent)
{
	qInfo() << "test class Created!" << endl;
}

test::~test()
{
	qInfo() << "test class destructed!" << endl;
}

이미지

⚠️ **GitHub.com Fallback** ⚠️