F
forums_mp
Need guidance on a viable solution with regards to handling global
data – specifically data pertaining to mathematical constants among
other things - within application code. Thus far I’ve arrived at
three approaches.
a) Approach 1
// global_file.h
#ifndef GLOBAL_FILE
#define GLOBAL_FILE
static int const myGlobal1 = 0xCAFE ;
static double const PI = 3.1415926535897932384626433832795;
#endif
Approach 1 is akin to an unnamed (anonymous) namespace. Each
translation unit gets a copy of the member data. Simply not
acceptable with regards to the static initialization fiasco etc that
the FAQ highlights
b) Approach II - The ‘C’ solution. Make use of the extern keyword
// global_file.h
#ifndef GLOBAL_FILE
#define GLOBAL_FILE
extern int const myGlobal1;
extern double const PI ;
#endif
//global_file.cpp
int const Global1 = 0xCAFE;
double const PI = 3.1415926535897932384626433832795 ;
c) Approach III - use a class – make members public. (or_ make
member data private and use a “get/accessor’ function to access the
private data)
///global.h
class global {
foo()
: PI ( 3.141 )
{}
public:
double const PI ;
static int const Global1 = 0xCAFE ;
};
Approach III is what I’m leaning towards.
data – specifically data pertaining to mathematical constants among
other things - within application code. Thus far I’ve arrived at
three approaches.
a) Approach 1
// global_file.h
#ifndef GLOBAL_FILE
#define GLOBAL_FILE
static int const myGlobal1 = 0xCAFE ;
static double const PI = 3.1415926535897932384626433832795;
#endif
Approach 1 is akin to an unnamed (anonymous) namespace. Each
translation unit gets a copy of the member data. Simply not
acceptable with regards to the static initialization fiasco etc that
the FAQ highlights
b) Approach II - The ‘C’ solution. Make use of the extern keyword
// global_file.h
#ifndef GLOBAL_FILE
#define GLOBAL_FILE
extern int const myGlobal1;
extern double const PI ;
#endif
//global_file.cpp
int const Global1 = 0xCAFE;
double const PI = 3.1415926535897932384626433832795 ;
c) Approach III - use a class – make members public. (or_ make
member data private and use a “get/accessor’ function to access the
private data)
///global.h
class global {
foo()
: PI ( 3.141 )
{}
public:
double const PI ;
static int const Global1 = 0xCAFE ;
};
Approach III is what I’m leaning towards.