static and global variable

  • Thread starter ravinder thakur
  • Start date
R

ravinder thakur

hi all experts,

i am porting a project from c to the c++ framework. during the porting
i have
encounterd a where i have a static and a global variable with the same
name. now what rules c languages uses to reslolve the ambiguity between
these two variables with the same names. more specifically what will
happen if the both of these varaibles gets included in some compilation
unit due to the interdependency between the header files???


plz note that this code is compiling and
linking perfectly but not in the newer c++ framework.



thanks in advance
rt
 
A

Alexei A. Frounze

ravinder thakur said:
i have
encounterd a where i have a static and a global variable with the same
name. now what rules c languages uses to reslolve the ambiguity between
these two variables with the same names. more specifically what will
happen if the both of these varaibles gets included in some compilation
unit due to the interdependency between the header files???

If you don't tell anything about the global variable to the unit with the
static variable, there will be no problem. Otherwise, if the unit *sees*
that global variable (through inclusion of something with "extern type
globalVariable;"), then the compiler must issue at least a warning if not
error. If it doesn't, there's a problem...
The simplest solution is to rename one of the variables, most probably the
static one as this change would affect just one compilation unit, whereas
renaming the global variable will introduce changes in all its users.

However, if there's a local variable and a static/global variable having the
same name, this would be OK and the local one will be given favor and used.

Alex
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top