I use Qt, but this is a general question in C ++. My case is simple, I have a class Constantsthat has a constant static member that I want it to be initialized after making certain function calls.
Constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
class Constants
{
public:
static const char* const FILE_NAME;
};
#endif
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;
return a.exec();
}
When compiling, I got:
QCoreApplication :: applicationFilePath: first instantiate the QApplication object
So, the problem here is obvious. When the static QApplication function is called in Constants.cpp, QApplication has not yet been installed by Qt. I need to somehow wait while in main.cpp
no string will be transmitted QApplication a(argc, argv);Is it possible, and if not what else could you offer to overcome this?
thank