Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ is it possible to delay initialization of constant static member?

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

like image 671
destan Avatar asked Oct 18 '11 11:10

destan


1 Answers

Typical solution:

#ifndef CONSTANTS_H
#define CONSTANTS_H

class Constants
{
public:

    static const char* const getFILE_NAME();
};

#endif // CONSTANTS_H

And in cpp

#include "constants.h"
#include <QApplication>

const char* const Constants::getFILE_NAME()
{
    static const char* const s_FILE_NAME = QApplication::applicationFilePath().toStdString().c_str();

    return s_FILE_NAME;
}
like image 163
sehe Avatar answered Sep 30 '22 01:09

sehe