Click here to Skip to main content
15,867,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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
Updated 2-Nov-11 18:56pm
v2

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
 
Share this answer
 
Comments
RaisKazi 4-Nov-11 3:15am    
My 5! Seems OP got the Answer. I am happy too. :)
Sergey Alexandrovich Kryukov 4-Nov-11 13:24pm    
Thank you, Rais.
--SA
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:

C++
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?
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Nov-11 1:29am    
This is another part of it, a 5. I explained the static vs. instance (non-static) part.
--SA
RaisKazi 4-Nov-11 3:16am    
It's really educating answer. My 5. :)
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
 
Share this answer
 
v2
Comments
RaisKazi 3-Nov-11 0:14am    
Agree 5+! 5 for Answer and + for - "What's the difference between apple and Apple?" :).
Sergey Alexandrovich Kryukov 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 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 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 3-Nov-11 11:14am    
That's very true Albert. Thank you.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900