Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any other possiblitly to prove this point other than using 'extern' keyword :n3290 draft

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

like image 775
user751747 Avatar asked Jul 26 '11 05:07

user751747


People also ask

What happens if we remove the extern keyword and why?

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.

Is extern keyword necessary?

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 extern variable is used in a file pick one option?

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.

Can we use extern and static keywords together?

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.


1 Answers

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

like image 157
Nawaz Avatar answered Oct 23 '22 09:10

Nawaz