initialising static member with static methode

  • Thread starter Jens Henrik Goebbert
  • Start date
J

Jens Henrik Goebbert

Hi,

I have some kind of tricky question on the c++ internals.
There must be some rule in what order static members get initialised.

I need to initialise a static member (m_sPropertyInfos) using a static
methode (initStaticPropertyInfos). This static methode uses a static
member staticMetaObject of the superclass.

Will that work with all compilers (it is fine with gcc-4.x)

myfile.cpp:
/*static*/ QList<AXPropertyInfo*> AXData::initStaticPropertyInfos() {
QList<AXPropertyInfo*> propertyInfos;
for(int i=0; i<staticMetaObject.propertyCount(); i++) {
propertyInfos.append(0);
}
return propertyInfos;
}

QList<AXPropertyInfo*> AXData::m_sPropertyInfos = initStaticPropertyInfos();

Greetings
Jens
 
V

Victor Bazarov

Jens said:
I have some kind of tricky question on the c++ internals.
There must be some rule in what order static members get initialised.

I need to initialise a static member (m_sPropertyInfos) using a static
methode (initStaticPropertyInfos). This static methode uses a static
member staticMetaObject of the superclass.

Will that work with all compilers (it is fine with gcc-4.x)

myfile.cpp:
/*static*/ QList<AXPropertyInfo*> AXData::initStaticPropertyInfos() {
QList<AXPropertyInfo*> propertyInfos;
for(int i=0; i<staticMetaObject.propertyCount(); i++) {
propertyInfos.append(0);
}
return propertyInfos;
}

QList<AXPropertyInfo*> AXData::m_sPropertyInfos =
initStaticPropertyInfos();

See the FAQ about "static object initialization fiasco".

V
 
J

James Kanze

I have some kind of tricky question on the c++ internals.
There must be some rule in what order static members get initialised.

Sort of. There are actually a lot of rules: everything gets
"zero initialized" first, then static initialization occurs,
then dynamic initialization. Dynamic initialization occurs in
the order of definition within a translation unit; the order of
dynamic initialization between translation units is unspecified.

Typically, it's that last rule that causes people problems, and
requires extensive work-arounds.
I need to initialise a static member (m_sPropertyInfos) using a static
methode (initStaticPropertyInfos). This static methode uses a static
member staticMetaObject of the superclass.

If the initialization of m_sPropertyInfos uses a function, it is
dynamic initialization. If staticMetaObject also requires
dynamic initialization, and is defined in a different
translation unit, you have a problem.
Will that work with all compilers (it is fine with gcc-4.x)
myfile.cpp:
/*static*/ QList<AXPropertyInfo*> AXData::initStaticPropertyInfos() {
QList<AXPropertyInfo*> propertyInfos;
for(int i=0; i<staticMetaObject.propertyCount(); i++) {
propertyInfos.append(0);
}
return propertyInfos;
}
QList<AXPropertyInfo*> AXData::m_sPropertyInfos = initStaticPropertyInfos();

Unless staticMetaObject has static initialization (trivial
constructor, only constant expressions used in a {...} style
initialization), this doesn't reliably work with any compiler I
know, including g++ 4.1.0, unless staticMetaObject is defined in
the same translation unit. Whether it happens to work on any
given day will typically depend on link order, which, if the
object files are in a library, may vary from one link to the
next.

You probably need to use some variant of the singleton idiom for
staticMetaObject.
 
S

silverdirk

Jens said:
Hi,

I have some kind of tricky question on the c++ internals.
There must be some rule in what order static members get initialised.

I need to initialise a static member (m_sPropertyInfos) using a static
methode (initStaticPropertyInfos).

As an alternative, you can use a static 'local' of a static function
instead of a static field.

myfile.cpp:
/*static*/ QList<AXPropertyInfo*> AXData::initStaticPropertyInfos() {
QList<AXPropertyInfo*> propertyInfos;
for(int i=0; i<staticMetaObject.propertyCount(); i++) {
propertyInfos.append(0);
}
return propertyInfos;
}

/* static*/ QList<AXPropertyInfo*>& GetStaticPropertyInfos() {
static QList<AXPropertyInfo*> propertyInfo=
initStaticPropertyInfos();
return propertyInfo;
}

This is a tiny bit less efficient than a normal static field, but
doesn't initialize until the method is called, which in most cases is
what you want.

In order to make sure it is initialized after staticMetaObject, you
can use the same design again: GetStaticMetaObject().propertyCount()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top