A point from ISO draft N3290 : Unqualified Name Lookup :section 3.4.1, para 14:
If a variable member of a namespace is defined outside of the scope of its namespace then any name that appears in the definition of the member (after the declarator-id) is looked up as if the definition of the member occurred in its namespace.
ex:
namespace N {
int i = 4;
extern int j;
}
int i = 2;
int N::j = i; // N::j == 4
Is there any other possiblitly to prove this point other than using 'extern' keyword
can u give some other examples ... other than extern
If it were, memory would never be allocated for them. Therefore, we need to include the extern keyword explicitly when we want to declare variables without defining them.
Declaration can be done any number of times but definition only once. “extern” keyword is used to extend the visibility of variables/functions(). Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition.
Which condition is true if the extern variable is used in a file? Explanation: Only one header file should declare the extern variable to be used. There must not be more than one file declaring the same extern variable. This is to ensure that there is no ambiguity in using the extern variable.
Thus, prefixes “ extern ” and “ static ” cannot be used in the same declaration. They maintain their value throughout the execution of the program independently of the scope in which they are defined.
Another example which doesn't use extern
keyword:
//demo.cpp
int i = 100;
namespace N
{
int i = 200;
struct A
{
static int m;
};
}
int N::A::m = i; //i is 100 or 200?
int main()
{
cout << N::A::m << endl; //what will it print?
}
Output:
200
Online demo : http://www.ideone.com/pRVAb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With