S
S James S Stapleton
I have some code, and I want to make it future-resistant. I have a bunch of
variables that are set up run-time, and once set up, should act as
constants. I don't want to #define them, because their values are determined
during the execution of the initialization routines, so I made them const
(I'd rather making it hard for a dev to accidentally overwrite the values).
The problem is, they are being set run-time by the return of a function
call. Right now my solution is ugly, and required me to turn a .c file to a
..cpp file. Any suggestions on how to do this in a better manner? I couldn't
find it via google or my reference books.
Basically I'm trying to make a variable non-const for one statement.
ex:
in the .h
extern const type_id CHAR_TYPE;
in the .cpp which I'd rather be a .c
const type_id CHAR_TYPE;
/*...*/
void mylib_init()
{
/*note the addressing and deadressing ops, const_cast requries pointers
to work...*/
*(const_cast<type_id*>(&CHAR_TYPE)) = ctypeless_register(sizeof(char),
NULL, NULL, "char");
}
so, if you are like me, you see heaploads of problems with my solution. Any
suggestions for a fix that is (a) less ugly, (b) doesn't require the use of
C++, and (c) makes sure people who use the code get warnings/errors if they
try to change the values of these variables.
Thanks,
-Jim Stapleton
variables that are set up run-time, and once set up, should act as
constants. I don't want to #define them, because their values are determined
during the execution of the initialization routines, so I made them const
(I'd rather making it hard for a dev to accidentally overwrite the values).
The problem is, they are being set run-time by the return of a function
call. Right now my solution is ugly, and required me to turn a .c file to a
..cpp file. Any suggestions on how to do this in a better manner? I couldn't
find it via google or my reference books.
Basically I'm trying to make a variable non-const for one statement.
ex:
in the .h
extern const type_id CHAR_TYPE;
in the .cpp which I'd rather be a .c
const type_id CHAR_TYPE;
/*...*/
void mylib_init()
{
/*note the addressing and deadressing ops, const_cast requries pointers
to work...*/
*(const_cast<type_id*>(&CHAR_TYPE)) = ctypeless_register(sizeof(char),
NULL, NULL, "char");
}
so, if you are like me, you see heaploads of problems with my solution. Any
suggestions for a fix that is (a) less ugly, (b) doesn't require the use of
C++, and (c) makes sure people who use the code get warnings/errors if they
try to change the values of these variables.
Thanks,
-Jim Stapleton