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;
}