Search for "Lazy initialization"
Basically is to defer the initialization of your object until you need it.
In your case, I'd use a pointer instead of your atribute
static A* a;
And initialize it with a NULL pointer.
You must give access to a through a getter method, returning a const
reference to a, instead of the pointer you have.
Protect your attribute inside the private section but let the getter in
the public section.
The code will be something like this:
class B {
static const A* a;
public:
static const A& getA() {
if(!a)
a=new A();
return *a;
}
};
// in B.cpp
const A* B::a=NULL;
mihai wrote:
the code is someting like this ...
class A
{
public:
A()
{
if bad thing happens
throw exception;
}
...
};
class B
{
public:
static const A a;
};
// in B.cpp
const A B::a;
here is the problem ... :(
if A() throws the exception ... the application will creash...