我使用Qt在C++和行爲枚舉正在與一個枚舉掙扎。考慮像下面這樣的情況:C++ - 在成員對象
克隆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
與ErrorType
類型的變量mError
工作不能按預期工作(它在執行過程中呈現奇怪的和不一致的值)。
我得到的輸出是這樣的:
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.
後凡'mError'初始化一個完整的例子 – 2013-03-28 05:24:10
? – user93353 2013-03-28 05:31:11
它確實看起來像初始化錯誤。你應該這樣寫:'mError = NoError;'在你的MemberOfMyClass構造函數中。 – Amartel 2013-03-28 06:48:22