在Qt文档中,有一个关于qobject_cast使用的示例.
按照这个例子,我试过这个:
#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();
}
Run Code Online (Sandbox Code Playgroud)
此代码编译但在运行时,程序崩溃在行:
QTimer *timer = qobject_cast<QTimer *>(obj);
Run Code Online (Sandbox Code Playgroud)
调试器说这是由于分段错误造成的qobject_cast.我不明白问题出在哪里.所以我有一个问题,为什么有一个seg.错?