views:

56

answers:

2

HI,

i have small doubt related to the accessibility of variables.

int i; //default the linkage is external
const int i; //default linkage is internal
extern int i; //explicitly assigning linkage as external



class a
    {
        int l;  //Default linkage is external
        void f() 
        {
          int k; //default linkage is external
        }
    }

this link says default linkage is extern for non-const symbols and static (internal) for const symbols.

what about int i is it accessible in other file with out having external keyword? what about the variable present inside the class and functions?

How to aces the function present in anonymous name space & what linkage do they have?

namespace //members of anonymous namespace
{
 class C{};
 int x;
 }
A: 

Refer here

Names in unnamed namespace have internal linkage.

Chubsdad
Hi, thanks,what about the variables declared with in the function, they will have no linkage or what?
Shadow
@chubsdad: No they don't. Names in an unnamed namespace have the same linkage as they would in any other namespace: external, or internal for constant data.
Mike Seymour
@Mike Seymour: Yes, you are right. A footnote says "Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit."
Chubsdad
+1  A: 

int i; has external linkage, and is in a normal namespace, so it is accessible from other files. They will have to declare it extern int i; in the same namespace (in this case, the global namespace) to access it.

Members of an unnamed namespace are not accessible from other files - that's the purpose of the namespace. Although they can have external linkage, their namespace is unique to the current file, so the same declarations in another file will refer to different things, unique to that file.

Mike Seymour