좋아요, 그렇기 때문에 Qt와 C++을 처음 접해 보았습니다. 내 자신의 클래스와 함께 QMetaType을 사용하려고하는데 하위 클래스와 함께 사용할 수 없습니다.QMetaType 및 상속
testparent.h : : 여기가 무슨 (아마도 문제의 톤, 미안 거기)
#include <QMetaType>
class TestParent
{
public:
TestParent();
~TestParent();
TestParent(const TestParent &t);
virtual int getSomething(); // in testparent.cpp, just one line returning 42
int getAnotherThing(); // in testparent.cpp, just one line returning 99
};
Q_DECLARE_METATYPE(TestParent)
을 그리고 여기 test1.h를이다 :
#include <QMetaType>
#include "testparent.h"
class Test1 : public TestParent
{
public:
Test1();
~Test1();
Test1(const Test1 &t);
int getSomething(); // int test1.cpp, just one line returning 67
};
Q_DECLARE_METATYPE(Test1)
(그렇지 않는 한 여기에 선언 된 모든 멤버는 testparent.cpp 또는 test1.cpp에서 아무 것도 처리하지 않기를 정의합니다 (testparent.cpp 또는 test1.cpp에서 대괄호를 열고 닫음). main.cpp :
#include <QtGui/QApplication>
#include "test1.h"
#include "testparent.h"
#include <QDebug>
int main(int argc, char *argv[])
{
int id = QMetaType::type("Test1");
TestParent *ptr = new Test1;
Test1 *ptr1 = (Test1*)(QMetaType::construct(id));
// TestParent *ptr2 = (TestParent*)(QMetaType::construct(id));
qDebug() << ptr->getSomething();
qDebug() << ptr1->getSomething(); // program fails here
// qDebug() << ptr2->getAnotherThing();
// qDebug() << ptr2->getSomething();
delete ptr;
delete ptr1;
// delete ptr2;
return 0;
}
당신이 볼 수 있듯이 ptr2로 일부 다형성 물건을 테스트하려했지만 ptr1이 작동하지 않는다는 것을 깨달았습니다. (편집 : 전 문장이 의미가 없습니다. 오, 잘 해결 된 문제 (수정 : nvm 그것은 이해가)) 내가 이것을 실행하면 어떻게됩니까 처음으로 qDebug 67, 예상대로 인쇄 및 몇 초 동안 붙어 도착하고 결국 코드 -1073741819로 종료됩니다.
고마워요.
감사합니다 순전히! 나는 어리 석다. – JohnJamesSmith0