Click to See Complete Forum and Search --> : C++ problem
nmayotte
12-07-2002, 01:26 AM
Is there a way to initialize a variable in a header file, without making it const static, because I will need to increment it later.
The problem I am running into is that for this project, I cannot edit the main driver file, only the header and definition files. And I need a variable that initially gets initialized, but not every time after that when the default constructor is called.
Does that make sense? I can explain it in more detail if neccessary.
Conrad Song
12-07-2002, 01:46 AM
Originally posted by nmayotte
Is there a way to initialize a variable in a header file, without making it const static, because I will need to increment it later.
The problem I am running into is that for this project, I cannot edit the main driver file, only the header and definition files. And I need a variable that initially gets initialized, but not every time after that when the default constructor is called.
Does that make sense? I can explain it in more detail if neccessary.
You can declare a variable 'static' without the 'const' prefix. However, declaring a 'static' variable in the header does not actually create one. You then need to define the actual variable in a source file.
gmleo2003
12-07-2002, 05:54 PM
hey man.. did u try to declare the variable with the keyword "extern" in front of const int......... would look for e.g. like:
extern int myvar;
declare it like this in the library headaer and will be accessable anywhere else.. please let me know if it worked...:cool:
TPoise
12-16-2002, 01:05 AM
Well if you can't edit the header files from your project, then most likely you can do it without editing it. But if you MUST go about your way of doing things... Use the extern feature in C++, it lets you create a "global" variable across multiple files that you can edit later. Google extern for more reference or your C++ book
peterlak
12-19-2002, 09:54 PM
no