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<Thingxx;
static
{
xx = new Vector<Thing>(4 2);
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_cha nnels_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_in itializer();
}
VPNChannel.cpp
--------------
#include "VPNChannel .h"
uint32_t VPNChannel::smA llocated_channe ls_map(static_c lass_initialize r());
uint32_t VPNChannel::sta tic_class_initi alizer()
{
for (int i=0; i < MAX_CHANNELS; ++i)
{
VPNChannel::smI nstances[i] = (VPNChannel *)NULL;
}
return (uint32_t)0;
}
with this idea in mind:
static_class_in itializer() 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_ch annels_map).
To my surprise, this compiles fine, but gives a linker error:
VPNChannel.o(.t ext+0x19):VPNCh annel.cpp: undefined reference to
`VPNChannel::sm Instances'
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
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<Thingxx;
static
{
xx = new Vector<Thing>(4 2);
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_cha nnels_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_in itializer();
}
VPNChannel.cpp
--------------
#include "VPNChannel .h"
uint32_t VPNChannel::smA llocated_channe ls_map(static_c lass_initialize r());
uint32_t VPNChannel::sta tic_class_initi alizer()
{
for (int i=0; i < MAX_CHANNELS; ++i)
{
VPNChannel::smI nstances[i] = (VPNChannel *)NULL;
}
return (uint32_t)0;
}
with this idea in mind:
static_class_in itializer() 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_ch annels_map).
To my surprise, this compiles fine, but gives a linker error:
VPNChannel.o(.t ext+0x19):VPNCh annel.cpp: undefined reference to
`VPNChannel::sm Instances'
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
Comment