Linkage Types

Objects, references, functions, types, templates, and namespaces are all considered names. A name can have one of three linkage types: external linkage, internal linkage, or no linkage. The linkage type of a name specifies its visibility from other scopes and translation units. A name with external linkage can be referred to from every translation unit of the program. Examples of such names include ordinary functions that aren't explicitly declared as static, global objects, const objects explicitly declared extern, classes, enumerations and their enumerators, templates, namespaces, and so on. Here are a few examples of names with external linkage:

 

int  n;  // global non-static, hence external linkage
class  C
{
 
void  f();  //  member functions
  static   int  n; //  static data members
};
extern   const  K;  // defined in a different translation unit
void  func ();
namespace  NS
{
 
class  D{};  //  qualified name NS::D has external linkage
}
enum  DIR
{
 Up,
 Down
//  DIR, Up, and Down have external linkage

A name with internal linkage is visible only from within the translation unit in which it was declared. A name declared in a namespace scope (that is, declared globally or within a namespace) has internal linkage if it's the name of a static object, a static function, a member of an anonymous union, a member of an anonymous namespace, a typedef name, or a const object not declared extern. Here are some examples of names with internal linkage:

 

static   void  f();  // a static function
static   int  q;  // a static object declared in global scope
namespace   // members of anonymous namespace
{
 
class  C{};
 
int  x;
 }
const  M = 1000 // const object not declared extern
union{  // members of an anonymous union
  int  x;
 
float  y;
};
typedef 
int  I;  //  typedef names

Names with no linkage are visible only in the scope in which they're declared. Such names include local objects, local classes, and other local types. Put differently, any name that has neither external linkage nor internal linkage has no linkage. Here are some examples of such names:

int  main()
{
 
class  C{};  //  C is a local class; has no linkage
  int  j;  //  local object not declared extern has no linkage
 C c;  //  the object c has no linkage
  enum  Parity  //  local enum and enumerators
 {
 Even,
 Odd
 };
}

 

 原文章地址: http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=41&rl=1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值