Mimicking Javas static class initializer in C++
Hi folks,
as the subject says, i'm a poor Java programmer trying to transfer some
of his wisdom into C++ world... here is what im trying to do this evening:
Java has a nifty feature called a static class initializer - something
like this:
public class Foo
{
private static Vector<Thing> xx;
static
{
xx = new Vector<Thing>(42);
for (int i=0; i < xx.size(); ++i)
xx.set(i, new Thing());
}
}
where the static{} block is called once (when the class is first used),
thus suitable for complex initializations of static members.
Now i would like to have this in C++, can this be done?
Here is my current approach:
VPNChannel.h
------------
class VPNChannel
{
static const int MAX_CHANNELS = 4;
// Bitmask of allocated channels
static uint32_t smAllocated_channels_map;
//
// Vector holding all possible instances of VPNChannel
//
static VPNChannel *smInstances[MAX_CHANNELS];
//
// Static initializer
// This is kinda kludge to mimic Javas static {} class initializer
//
static uint32_t static_class_initializer();
}
VPNChannel.cpp
--------------
#include "VPNChannel.h"
uint32_t VPNChannel::smAllocated_channels_map(static_class_initializer());
uint32_t VPNChannel::static_class_initializer()
{
for (int i=0; i < MAX_CHANNELS; ++i)
{
VPNChannel::smInstances[i] = (VPNChannel *)NULL;
}
return (uint32_t)0;
}
with this idea in mind:
static_class_initializer() is used to do complex initialization of an
static array, it returns a dummy zero value, so it can be used to
initialze another static variable (smAllocated_channels_map).
To my surprise, this compiles fine, but gives a linker error:
VPNChannel.o(.text+0x19):VPNChannel.cpp: undefined reference to
`VPNChannel::smInstances'
collect2: ld returned 1 exit status
(I'm using MinGW 3.4.2)
Any hint, what goes wrong here? Or maybe a better, more c++ish approach
to static initialization?
Cheers and Greetings
Andreas