In the Qt documentation, there is an example on the use of qobject_cast.
Following this example, I tried this:
#include <QApplication>
#include <QTimer>
#include <QDebug>
void test_cast(QObject *returnedObject)
{
QTimer *timer = new QTimer;
timer->setInterval(500);
returnedObject = timer;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QObject *obj;
test_cast(obj);
QTimer *timer = qobject_cast<QTimer *>(obj);
if(timer)
qDebug() << timer->interval(); //Should be 500
return a.exec();
}
This code compiles but at runtime, the program crash at line:
QTimer *timer = qobject_cast<QTimer *>(obj);
The debugger says this is due to a segmentation fault in qobject_cast
.
I don't understand where is the problem. So I have a question, why there is a seg. fault ?