I am using Qt but this is a generic C++ question. My case is simple, I have a class Constants
which has a constant static member which I want it to be initialized after certain function calls are made.
Constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
class Constants
{
public:
static const char* const FILE_NAME;
};
#endif // CONSTANTS_H
Constants.cpp
#include "constants.h"
#include <QApplication>
const char* const Constants::FILE_NAME = QApplication::applicationFilePath().toStdString().c_str();
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "constants.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<< "name: "<<Constants::FILE_NAME;
//for those who are unfamiliar with Qt, qDebug just prints out
return a.exec();
}
When compiling I got:
QCoreApplication::applicationFilePath: Please instantiate the QApplication object first
So problem here is obvious. When QApplication's static function is called in Constants.cpp QApplication is not installed by Qt yet. I need to somehow wait until QApplication a(argc, argv);
line is passed in main.cpp
is it possible and if not what else could you suggest to overcome this?
thanks