c++ - Defining const static that needs setting up at runtime -


i'm making use of class few utilities defined static methods eg.

qdate ssimutils::convertssimdate(qstring s) {     qdate rtndt;     //...conversion code     return rtndt; } 

i define few constants in class eg. low_date , thinking of putting in like

const static qdate low_date; // need set somewhere 1/1/1970 

unfortunately, can't define pre-compile time int eg.

const static int ssimutils::myi = 4; 

because requires use of setdate method.

my question how should define static const need set codewise, constant requires initialisation. i'd been thinking of defining in .h file eg.

const static qdate low_date; 

then in .cpp file, @ top, doing like

ssimutils::low_date.setdate(1970,1,1); 

but syntactically incorrect. i'd use constant in other classes eg.

if (myqdate.compare(ssimutils::low_date)==0) {     // something. } 

what's right way set constant value in static class need adjust @ run time ie. constructor?

as mentioned in comment, qdate has constructor equivalent setdate(), allows initialisation of 'const' object.

you must declare static constant following way:

myclass.h:

#include <qdate>  class myclass { public:     const static qdate const_date; }; 

myclass.cpp:

#include "myclass.h"  const qdate myclass::const_date(1970, 1, 1); 

i tested using std::string instead of qdate (no qt available right now), , works want.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -