 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eric Lilja Guest
|
Posted: Wed Jan 26, 2005 12:18 pm Post subject: Warning about defined but not used for global variable |
|
|
Hello, I have a few global variables in my program. One of them holds the
name of the application and it's defined in a header file globals.hpp (and
the point of definition also happen to be the point of declaration of this
variable, correct?):
static const char * g_application_name = "Tiny, class-based MDI Example";
In another source file, I'm including the header globals.hpp and I'm using
the variable g_application_name. I do, however, get the following warning
when building my program:
globals.hpp:12: warning: 'g_application_name' defined but not used
This warning is repeated for every file that includes globals.hpp, either
directly or indirectly.
However, if I change the type of g_application_name to const char * const,
the warning disappears.
So I guess that the proper way to define a global string variable that is
not to be written, only read, and should be shared for all source files is
to the define it with the type const char * const?
/ Eric
|
|
| Back to top |
|
 |
alexmdac@hotmail.com Guest
|
Posted: Wed Jan 26, 2005 12:30 pm Post subject: Re: Warning about defined but not used for global variable |
|
|
Yes you should use const char * const. const char * means that the
string buffer is writable but the pointer itself is constant. You
should treat string literals as read-only.
|
|
| Back to top |
|
 |
Matthias Guest
|
Posted: Wed Jan 26, 2005 1:17 pm Post subject: Re: Warning about defined but not used for global variable |
|
|
[email]alexmdac (AT) hotmail (DOT) com[/email] wrote:
| Quote: | Yes you should use const char * const. const char * means that the
string buffer is writable but the pointer itself is constant. You
should treat string literals as read-only.
|
Hm isn't it the other way around? AFAIK const char* means that the
character data is constant, but the pointer to it isn't, whereas char*
const means that the character data is variable and the pointer constant.
Correct me if I'm wrong.
--
Regards,
Matthias
|
|
| Back to top |
|
 |
scott urban Guest
|
Posted: Sun Jan 30, 2005 5:22 pm Post subject: Re: Warning about defined but not used for global variable |
|
|
On 2005-01-26, Eric Lilja <ericliljaNoSpam (AT) yahoo (DOT) com> wrote:
| Quote: | Hello, I have a few global variables in my program. One of them holds the
name of the application and it's defined in a header file globals.hpp (and
the point of definition also happen to be the point of declaration of this
variable, correct?):
static const char * g_application_name = "Tiny, class-based MDI Example";
|
Lose the static keyword; with static you get a separate variable named
'g_application_name' local to each translation unit that includes that
header - not 'global' at all. Change it to extern:
extern const char * g_application_name;
And, in one cpp file:
const char * g_application_name = "Tiny, class-based MDI Example";
| Quote: | In another source file, I'm including the header globals.hpp and I'm using
the variable g_application_name. I do, however, get the following warning
when building my program:
globals.hpp:12: warning: 'g_application_name' defined but not used
This warning is repeated for every file that includes globals.hpp, either
directly or indirectly.
However, if I change the type of g_application_name to const char * const,
the warning disappears.
|
I'm not sure what the rules are here - but for types like int:
const int name = 0;
in a header is used everywhere possibly in read only memory and doesn't
need to be set up like a global variable that might change. That
apparently applies to 'const char * const' too, at least for your
compiler.
| Quote: | So I guess that the proper way to define a global string variable that is
not to be written, only read, and should be shared for all source files is
to the define it with the type const char * const?
|
I think I would still do it the proper way, with a an extern declaration
in the header and a single definition in one cpp. At a minimum, if you
change the name of your application, you won't have to recompile all
files that include the header.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|