|
-
C++ problem
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.
-
Re: C++ problem
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.
-
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...
-
Expensive Sushi
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
-
Registered User
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|