Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing const values at develop-time without recompiling everything

Right now I have a considerable amount of would-be magic numbers stored in constants. These get tweaked a lot in between each build. eg const int numAPPLES = 25

right now these are each defined at the top of each associated class header. But sometimes they need to be shared and I and up having to either:

  1. Copy the definition; and risk conflicting values
  2. include extra header (which is what I've done)

I previously had them all in an options.h which is great because I can see them all together but changing a number would obviously trigger a complete rebuild.

If it matters I am using VS 2010.

What is the best way to allow changing these numbers? I am wondering how I could add a .txt file to my VS project and then extract the variables from there.

the data types are (for now) only char, and int

I also don't want to have a whole library dependency just to access them. But a simple recommended class; or way to build own would be great.

thanks!


edit: does this look good to you guys?: http://www.codeproject.com/KB/cpp/IniReader.aspx

like image 940
Zak Avatar asked Sep 08 '26 02:09

Zak


2 Answers

If you need the constants at compile time, you really have to recompile when they change.

If you only need the values at runtime, you can declare them as

extern const int numAPPLES;

and put the actual values in a separate .cpp file. When you change a value, you just have to recompile that one file.

like image 89
Bo Persson Avatar answered Sep 10 '26 15:09

Bo Persson


Have you considered using a .ini file which is read at runtime to store all these constants? You can then read such files using GetPrivateProfileInt/GetPrivateProfileString. As fas as my experience goes it is a pretty common way to deal with your situation under Windows.

Here's an example given a simple Try.ini file:

..
[Section1]
Const1 = 1
..

And the code snipper:

CString FileName = _T("Try.ini");
int Value = GetPrivateProfileInt( "Section1", "Const1", DEFAULT_ERROR, FileName );
if( Value == DEFAULT_ERROR)
  return ERROR;

Cheers

like image 38
JoeSlav Avatar answered Sep 10 '26 16:09

JoeSlav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!