我在 C++ 中使用 Qt 并且正在努力解决枚举问题。考虑一个类似下面的案例:
在 GitHub 上克隆:https ://github.com/jif/enum
// memberclass.h =======================================================
#ifndef MEMBERCLASS_H
#define MEMBERCLASS_H
#include <QObject>
class MemberClass : public QObject
{
Q_OBJECT
public:
enum ErrorType {
NoError,
IsError
};
explicit MemberClass(QObject *parent = 0);
void setError(ErrorType errorType);
MemberClass::ErrorType error() const;
void otherMethod();
private:
MemberClass::ErrorType mError;
};
#endif // MEMBERCLASS_H
// memberclass.cpp =======================================================
#include "memberclass.h"
#include <QDebug>
MemberClass::MemberClass(QObject *parent) :
QObject(parent)
{
mError = NoError;
qDebug() << "mError initialized.";
}
MemberClass::ErrorType MemberClass::error() const {
return mError;
}
void MemberClass::setError(ErrorType errorType) {
mError = errorType;
}
void MemberClass::otherMethod() {
qDebug() << " In otherMethod()...";
qDebug() << " mError = " << mError;
qDebug() << " NoError = " << NoError;
qDebug() << " IsError = " << IsError;
qDebug() << " End otherMethod()";
}
// parentclass.h =======================================================
#ifndef PARENTCLASS_H
#define PARENTCLASS_H
#include <QObject>
#include "memberclass.h"
class ParentClass : public QObject
{
Q_OBJECT
public:
explicit ParentClass(QObject *parent = 0);
void testEnumStuff();
private:
MemberClass objectMember;
MemberClass *pointerMember;
};
#endif // PARENTCLASS_H
// parentclass.cpp =======================================================
#include "parentclass.h"
#include <QDebug>
ParentClass::ParentClass(QObject *parent) :
QObject(parent)
{
pointerMember = new MemberClass(this);
}
void ParentClass::testEnumStuff() {
qDebug() << "Just initialized...";
qDebug() << " pointerMember::mError = " << pointerMember->error();
qDebug() << " objectMember::mError = " << objectMember.error();
qDebug() << "Calling otherMethod() on each member...";
qDebug() << " In pointerMember...";
pointerMember->otherMethod();
qDebug() << " In objectMember...";
objectMember.otherMethod();
qDebug() << " pointerMember::mError = " << pointerMember->error();
qDebug() << " objectMember::mError = " << objectMember.error();
qDebug() << "Done.";
}
// main.cpp =======================================================
#include <QCoreApplication>
#include "parentclass.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ParentClass parent;
parent.testEnumStuff();
return a.exec();
}
// enum.pro =======================================================
QT += core
QT -= gui
TARGET = enum
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
parentclass.cpp \
memberclass.cpp
HEADERS += \
parentclass.h \
memberclass.h
mError
使用类型变量ErrorType
不能按预期工作(它在执行过程中会出现奇怪且不一致的值)。
我得到这样的输出:
mError initialized.
mError initialized.
Just initialized...
pointerMember::mError = 0
objectMember::mError = 0
Calling otherMethod() on each member...
In pointerMember...
In otherMethod()...
mError = 0
NoError = 0
IsError = 1
End otherMethod()
In objectMember...
In otherMethod()...
mError = 13498688
NoError = 0
IsError = 1
End otherMethod()
pointerMember::mError = 0
objectMember::mError = 13498688
Done.