Based on my understanding of COMMON from this page, the C++ equivalent would be to make a file called common.h
(with include guards) that contains:
namespace BLK1
{
int const Gw = 200;
int const Eta = 4096;
int const t = 4096;
int const Phi = 200;
int const w = 200;
}
namespace BLK2
{
extern int g, dw, Vel, M, dt, N, Ioutp1, Ioutp2;
}
namespace BLK3
{
extern int Hs, Std, E, Hs1, Tdt;
}
Also, in exactly one .cpp
file in your project you need to provide a definition for any non-consts, e.g. in foo.cpp
:
#include "common.h"
namespace BLK2
{
int g, dw, Vel, M, dt, N, Ioutp1, Ioutp2;
}
namespace BLK3
{
int Hs, Std, E, Hs1, Tdt; // initialized to 0 by default
}
You may want to use a different type than int
, e.g. unsigned long
. I'm assuming the initialized values are meant to be const; if not then change int const
to extern int
and remove the initializer. The initializer would have to go in the definition in the .cpp
file.
Avoid the mistake of declaring a non-const, non-extern variable in the header; this causes undefined behaviour if the header is included in two different units.
You access these variables by writing BLK1::Eta
for example.
As you surmise it might be considered tidier to use a struct
instead of a namespace, although you'd still have to create an instance of the struct which is declared extern
in the header, and defined in exactly one .cpp
file; and if you are pre-C++11 it's more annoying to provide initializers.
(Of course, even better would be to refactor the code to not use globals. But it might be useful as a first pass to do a direct translation).
f2c -a
and the job is done. – David Heffernan