私は 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
typeの変数の操作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.