I have this in my C++ header file:
#include <QMap>
#include <QString>
class LogEvent {
public:
LogEvent();
enum column_t {TIMESTAMP_COLUMN = 0, TYPE_COLUMN = 1, EVENT_COLUMN = 2,
FILE_COLUMN = 3};
static QMap<column_t, QString> COLUMN_NAMES;
static QMap<column_t, QString> getColumnNames() {
return LogEvent::COLUMN_NAMES;
}
//blah blah blah other functions
};
This is my C++ source file:
#include "LogEvent.h"
LogEvent::LogEvent()
{
//constructor code
}
//blah blah blah other functions
I want to add values to my static QMap COLUMN_NAMES. Where and how would I do this?
You can use static function that returns initialized map:
// source file:
QMap<column_t, QString> LogEvent::initColumnNames() {
QMap<column_t, QString> map;
map.insert(LogEvent::TIMESTAMP_COLUMN,"Timestamp");
// and so on
return map;
}
QMap<column_t, QString> LogEvent::COLUMN_NAMES = initColumnNames();
Also, in case you want to use strings internationalization in static variables and need to call QTextCodec::setCodecForTr
first, it is a good idea to move static variable into its getter function:
class LogEvent {
public:
// ...
static QMap<column_t, QString> initColumnNames();
static QMap<column_t, QString> getColumnNames() {
static QMap<column_t, QString> COLUMN_NAMES = initColumnNames();
return COLUMN_NAMES;
}
}
This way COLUMN_NAMES
will be initialized with the first call of getColumnNames()
and you can set the text codec beforehand. But this is not thread-safe.