Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
extern int iNSIntl; //defined in other cpp
static int iNSIntl=2;
namespace{
    int iNSIntl=0;
}
void main ( int argc, char** argv )
{
    cout<<::iNSIntl<<endl;
}
In the main(),::iNSIntl refer to static one,how Can I refer to the extern one or within the namespace?
Posted 2-Nov-11 18:38pm
Edited 2-Nov-11 19:56pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To understand how static and non-static instance variables of the class work, please see my past solutions:
 
What makes static methods accessible?[^],
Catch 22 - Pointers to interface objects die when function using them is made static.[^].
 
Last of these two solutions is specific to C++. In C++, there is also such think as local static variable which is declared in the same context as a normal stack variable but in run-time has nothing to do with this context; it is stored in the area dedicated for static data (data segment of a process, in some memory models). The life time of the variable is nearly the lifetime of the application's process. When the execution exits the function where such variable is declared, its value is preserver. In contrast, a normal local variable is a stack variable: it points to some memory within the stack frame which logically does not even exist before the call (but physically it always exists as the stack is allocated after the program is loaded for a main thread and allocated for each new thread created at the moment of its creation), so the local variable should be initialized on every call.
 
—SA
  Permalink  
Comments
RaisKazi at 4-Nov-11 3:15am
   
My 5! Seems OP got the Answer. I am happy too. :)
SAKryukov at 4-Nov-11 13:24pm
   
Thank you, Rais.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think I understand your question (which was not worded correctly). You are basically wondering what the difference between the global and anonymous namespaces are. Consider this example:
 
namespace
{
	void Foo()
	{
	}
}
 
void Foo()
{
}
 
Now if you call Foo(), the compiler will be confused. They are both under equal callable scope for Foo. You will need to explicitly call ::Foo which calls the global method (the 2nd one). You cannot call the anonymous Foo here now. Now if you comment out the global Foo, then your call to Foo (no :: there) will work since the compiler will look in the anonymous namespace after it first looks for a global version.
 
Does that answer your question?
  Permalink  
v2
Comments
SAKryukov at 4-Nov-11 1:29am
   
This is another part of it, a 5. I explained the static vs. instance (non-static) part.
--SA
RaisKazi at 4-Nov-11 3:16am
   
It's really educating answer. My 5. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The question is not answerable because it is incorrect. These two notions has nothing to do with each other. Nothing at all.
 
Generally, the questions like "what is the difference between {0} and {1}" are not correct. Can't you see it? "What's the difference between apple and Apple?" Such question might have very limited sense when the compared entities are apparently close. But why would you think it's the case?
 
Static or non-static variables are related to functionality and run-time behavior of objects; it has nothing to do with visibility or access to the variable. As to name spaces, they exist only during compilation and only effect names and name resolution. These two aspects of programming never clash or affect each other. Any static of non-static object can be declared in any name space or default name space; and this will not affect its accessibility or behavior, only its naming.
 
You can find exact meaning and purpose of all of the above in any C++ manual. These notions are really the basic ones.
 
—SA
  Permalink  
v2
Comments
RaisKazi at 3-Nov-11 0:14am
   
Agree 5+! 5 for Answer and + for - "What's the difference between apple and Apple?" :).
SAKryukov at 3-Nov-11 0:27am
   
Thank you, Rais. Great to know that you understand it, too (about a type of question making no sense).
--SA
RaisKazi at 3-Nov-11 2:50am
   
Yup. But as Albert said it's bit of harsh. I removed my "Question makes no sense at all" comment, because it was harsh too. :)
Albert Holguin at 3-Nov-11 11:10am
   
Sometimes its hard not to be harsh on some people that post... but as long as they're making an effort to learn, that's what matters. I was a tutor in college and even in person I sometimes lost my cool with people.
RaisKazi at 3-Nov-11 11:14am
   
That's very true Albert. Thank you.
SAKryukov at 3-Nov-11 17:54pm
   
I have slightly different point of view on this topic. Life is harsh, really. One of the most important aspect of teaching is criticism and evaluation of the student's work, and, no matter if we want it or not, a student herself/himself. If a students need to be prepared to real life, she/he need to listen to all the ugly truth and learn to react adequately, either by improving or by standing for her/his opinion (because we all can make mistakes, too, but there is a positive moment in it: this is also a change for a student to learn how to stand for herself/himself).
 
I personally fix my mistakes no matter who is pointing to them: a school kid or a distinguished professor, but I somehow can stand for my opinion if I can see a mistake in the criticism, no matter who am I talking with. Really, there is enough evidence here at CodeProject. This is the only productive attitude. How one can come to this?
 
--SA
Albert Holguin at 3-Nov-11 19:40pm
   
We're all entitled to our opinions... :)
SAKryukov at 4-Nov-11 1:28am
   
We certainly are :-)
--SA
jiazhiqiang at 3-Nov-11 0:53am
   
thank for you reply."You can find exact meaning and purpose of all of the above in any C++ manual"But I have referred serveral manuals but they don't describe the "static" accurate meaning,or the different behaviour the compiler takes relative to no-static obj.Can you offer some? or a link!
And some articles about the compiler working principle.
thank you very much!
Albert Holguin at 3-Nov-11 11:08am
   
Try google.[^] Basically, static variables are only instantiated once during the execution of a program, even within a class. So they hold the value until the execution of the program ends (that's when they're defined as out of scope). That's just a little summary, the static keyword does however cover quite a few different things, so do some research. Good luck :)
SAKryukov at 3-Nov-11 17:45pm
   
Not so simple. Please take a look at my other solution on this page I created in response to this comment.
--SA
Albert Holguin at 3-Nov-11 19:39pm
   
That's why I said do some research... and you're topics include static methods, which are completely different from static variables. The static keyword covers quite a bit of ground.
SAKryukov at 3-Nov-11 17:34pm
   
This is strange, but... I'll help you. I have my own way to explain this thing in short. Let me find my past answers at CodeProject...
--SA
SAKryukov at 3-Nov-11 17:45pm
   
...Done. Please see my other solution on "static".
--SA
Albert Holguin at 3-Nov-11 0:46am
   
Although a bit harsh...correct... :)
SAKryukov at 3-Nov-11 0:48am
   
I know... Thank you, Albert.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 3 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100