设为首页 加入收藏

TOP

2.3 Compound Types (2)
2013-10-07 16:15:42 来源: 作者: 【 】 浏览:69
Tags:2.3 Compound Types

Reference Definitions

We can define multiple references in a single definition. Each identifier that is a referencemust be preceded by the & symbol:

  1. int i = 1024, i2 = 2048; // i and i2 are both ints   
  2. int &r = i, r2 = i2; // r is a reference bound to i; r2 is an int   
  3. int i3 = 1024, &ri = i3; // i3 is an int; ri is a reference bound to i3   
  4. int &r3 = i3, &r4 = i2; // both r3 and r4 are references 

With two exceptions that we’ll cover in § 2.4.1 (p. 61) and § 15.2.3 (p. 601), the type of a reference and the object to which the reference refersmust match exactly. Moreover, for reasons we’ll explore in § 2.4.1, a referencemay be bound only to an object, not to a literal or to the result of a more general expression:

  1. int &refVal4 = 10; // error: initializer must be an object   
  2. double dval = 3.14;   
  3. int &refVal5 = dval; // error: initializer must be an int object 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.2 Variables (9) 下一篇CHAPTER 2 VARIABLES AND BASICTY..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)