I was wondering why sometimes my global const defined in seperate .h file isn't properly initialized when I need it. Some tests lead me to situation that I can't understand. I don't know how to explain it, so here's the code:
main.cpp
#include <iostream>
#include "class.h"
using namespace std;
A a;
B b;
int main(int argc, char* argv[]){
A aa;
B bb;
cout<<a.a<<" "<<aa.a<<endl;
cout<<b.b<<" "<<bb.b<<endl;
return 0;
}
class.h
#ifndef CLASS_H
#define CLASS_H
#include "const.h"
class A {
public:
A();
float a;
};
class B {
public:
B():b(CONST){}
float b;
};
#endif
class.cpp
#include "class.h"
A::A()
: a(CONST){}
const.h
#ifndef CONST_H
#define CONST_H
#include <limits>
using namespace std;
const float CONST = numeric_limits<float>::has_infinity ?
-numeric_limits<float>::infinity() :
-numeric_limits<float>::max();
#endif
After running above code I get:
0 -1.#INF
-1.#INF -1.#INF
when actually I would like to get 4 times '-1.#INF'. Why does it happen this way? If CONST would be '1' instead of above formula, it would work perfectly.
I can "fix" it by making static getConst() method:
static float getConst(){
static const float CONST = numeric_limits<float>::has_infinity ?
-numeric_limits<float>::infinity() :
-numeric_limits<float>::max();
return CONST;}
but it just doesn't "feel" right. On other hand I just need two of those above... But maybe there's some other way?
And, most importantly, why class B gets "right" CONST and class A don't?