Warning: Defined but not used

W

William Payne

Hello, when compiling my program I get a warning in one of my header files
(globals.h) each time a source file includes it.
The warning reads:
globals.h:28: warning: `const char*g_mdi_child_class_name' defined but not
used
line 28 of globals.h is:
static const char* g_mdi_child_class_name = "MDIChildClass";

Why do I get this warning for this variable? It's used at three places
throughout the program. I have two static const int variables in globals.h
but I don't get any warnings regarding those. I also have five extern
variables.

/ WP
 
A

Andrey Tarasevich

William said:
Hello, when compiling my program I get a warning in one of my header files
(globals.h) each time a source file includes it.
The warning reads:
globals.h:28: warning: `const char*g_mdi_child_class_name' defined but not
used
line 28 of globals.h is:
static const char* g_mdi_child_class_name = "MDIChildClass";

Why do I get this warning for this variable? It's used at three places
throughout the program. I have two static const int variables in globals.h
but I don't get any warnings regarding those. I also have five extern
variables.
...

Since your variable is declared in a header file as 'static', each
translation unit that includes that header file will get its own "copy"
of this variable. Maybe in some of these translation units the variable
is indeed not used, which causes the above warning to appear. It's just
a guess though.
 
P

Phlip

William said:
Hello, when compiling my program I get a warning in one of my header files
(globals.h) each time a source file includes it.
The warning reads:
globals.h:28: warning: `const char*g_mdi_child_class_name' defined but not
used
line 28 of globals.h is:
static const char* g_mdi_child_class_name = "MDIChildClass";

Why do I get this warning for this variable? It's used at three places
throughout the program. I have two static const int variables in globals.h
but I don't get any warnings regarding those. I also have five extern
variables.

Focus on the 'static'. That means each translation unit (.cpp file) gets its
own copy of the variable (unless it's a compile-time constant, which it is
not, because the 'const' is on the left side of the star *).

So each of your translation units, except the three that use the variable,
get a copy of it that they don't use.

Your 'static const int' globals, by contrast, are compile-time constants, so
the compiler logically unifies them, and doesn't grant each translation unit
a separate one.

Write this:

static const char g_mdi_child_class_name[] = "MDIChildClass";

That declares intent better anyway. Nobody expected to re-point the former
pointer.
 
W

William Payne

Phlip said:
William said:
Hello, when compiling my program I get a warning in one of my header files
(globals.h) each time a source file includes it.
The warning reads:
globals.h:28: warning: `const char*g_mdi_child_class_name' defined but not
used
line 28 of globals.h is:
static const char* g_mdi_child_class_name = "MDIChildClass";

Why do I get this warning for this variable? It's used at three places
throughout the program. I have two static const int variables in globals.h
but I don't get any warnings regarding those. I also have five extern
variables.

Focus on the 'static'. That means each translation unit (.cpp file) gets its
own copy of the variable (unless it's a compile-time constant, which it is
not, because the 'const' is on the left side of the star *).

So each of your translation units, except the three that use the variable,
get a copy of it that they don't use.

Your 'static const int' globals, by contrast, are compile-time constants, so
the compiler logically unifies them, and doesn't grant each translation unit
a separate one.

Write this:

static const char g_mdi_child_class_name[] = "MDIChildClass";

That declares intent better anyway. Nobody expected to re-point the former
pointer.

Thanks Phlip, that made the warning disappear. Before I had declared those
variables extern instead of static, but I made them static instead because
my
program does not change their values, they are simply used to make code
clearer
(I hate magic numbers/strings).
Is this the proper way of declaring compile-time constants?

/ WP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top