c++ primer 学习之路 指针的引用

来源:互联网 发布:刀哥swift3.0源码 编辑:程序博客网 时间:2024/05/16 14:05

前些天看到一段代码,理解不清楚于是论坛发帖求助,高手们热情帮助下现在算是理解了一些,老师们给的答案内容丰富,在这总结整理一下,其中自己的一些理解如果有误,烦请您指出。

原问题地址:

http://bbs.csdn.net/topics/391830084


这段代码是在c++ primer 课后题答案中看到的:

int main(){// i is an int; p is a pointer to int; r is a reference to intint i = 1024, *p = &i, &r = i;// three ways to print the value of istd::cout << i << " " <<  *p <<  " " << r << std::endl;int j = 42, *p2 = &j;int *&pref = p2;  // pref is a reference to the pointer p2// prints the value of j, which is the int to which p2 pointsstd::cout << *pref << std::endl;// pref refers to a pointer; assigning &i to pref makes p point to ipref = &i; std::cout << *pref << std::endl; // prints the value of i// dereferencing pref yields i, the int to which p2 points; *pref = 0;  // changes i to 0std::cout << i << " " << *pref << std::endl;return 0;}


其中 p 为指向 i 的指针, r 为 i 的引用,修改 i 的值可以直接通过 i 进行修改,也可以解引用 p (*p) 进行修改, 也可以通过i 的引用 r 进行修改。 当其他函数调用 i 时,直接使用 i 传递的为 i 的拷贝,无法通过调用函数修改 i 的值,而通过指针和引用的方式则可以修改 i 的内容。

代码中:

int *&pref = p2;

定义了指针的引用,pref 为指针类型,且是指针 p2 的引用。 int * 为声明为指针类型,& 表明 pref 为 p2 的引用。此时pref 的值和 p2 的值相同,均为 j 的地址。

@lm_whales 老师给出的解释:

int *&pref = p2;  // pref is a reference to the pointer p2 定义指针的引用,C++特有的
这&只是引用的声明,&不能简单的看作运算符。
引用声明(定义)有个特点,
1)&后面不能再有 *了,引用没有对应的指针,因为引用的地址,就是对象的地址。
     只能定义指针的引用,不能定义引用的指针 
2) 引用不能定义数组,只能定义数组的引用,
3)C++11 右值引用 &&; 一个声明(定义)中 && 和 & 二者只能有一个,

引用和指针,同时用于声明(定义) 的时候,不存在优先级问题。
因为引用符号&,一定在指针符号 * 后面,
 这个引用符号和 标识符结合,表明这个标志符是个引用
然后这个引用,是对指针的引用 ,因为& 前面是 *。

引用和指针在一起 定义(声明)一个标识符,
这个标识符,一定是指针的引用,而不能是引用的指针。



另外,一开始我以为这涉及到运算符的优先级,有人给出了运算符优先级表:


//C++ Operators//  Operators specify an evaluation to be performed on one of the following://    One operand (unary operator)//    Two operands (binary operator)//    Three operands (ternary operator)//  The C++ language includes all C operators and adds several new operators.//  Table 1.1 lists the operators available in Microsoft C++.//  Operators follow a strict precedence which defines the evaluation order of//expressions containing these operators.  Operators associate with either the//expression on their left or the expression on their right;    this is called//“associativity.” Operators in the same group have equal precedence and are//evaluated left to right in an expression unless explicitly forced by a pair of//parentheses, ( ).//  Table 1.1 shows the precedence and associativity of C++ operators//  (from highest to lowest precedence).////Table 1.1   C++ Operator Precedence and Associativity// The highest precedence level is at the top of the table.//+------------------+-----------------------------------------+---------------+//| Operator         | Name or Meaning                         | Associativity |//+------------------+-----------------------------------------+---------------+//| ::               | Scope resolution                        | None          |//| ::               | Global                                  | None          |//| [ ]              | Array subscript                         | Left to right |//| ( )              | Function call                           | Left to right |//| ( )              | Conversion                              | None          |//| .                | Member selection (object)               | Left to right |//| ->               | Member selection (pointer)              | Left to right |//| ++               | Postfix increment                       | None          |//| --               | Postfix decrement                       | None          |//| new              | Allocate object                         | None          |//| delete           | Deallocate object                       | None          |//| delete[ ]        | Deallocate object                       | None          |//| ++               | Prefix increment                        | None          |//| --               | Prefix decrement                        | None          |//| *                | Dereference                             | None          |//| &                | Address-of                              | None          |//| +                | Unary plus                              | None          |//| -                | Arithmetic negation (unary)             | None          |//| !                | Logical NOT                             | None          |//| ~                | Bitwise complement                      | None          |//| sizeof           | Size of object                          | None          |//| sizeof ( )       | Size of type                            | None          |//| typeid( )        | type name                               | None          |//| (type)           | Type cast (conversion)                  | Right to left |//| const_cast       | Type cast (conversion)                  | None          |//| dynamic_cast     | Type cast (conversion)                  | None          |//| reinterpret_cast | Type cast (conversion)                  | None          |//| static_cast      | Type cast (conversion)                  | None          |//| .*               | Apply pointer to class member (objects) | Left to right |//| ->*              | Dereference pointer to class member     | Left to right |//| *                | Multiplication                          | Left to right |//| /                | Division                                | Left to right |//| %                | Remainder (modulus)                     | Left to right |//| +                | Addition                                | Left to right |//| -                | Subtraction                             | Left to right |//| <<               | Left shift                              | Left to right |//| >>               | Right shift                             | Left to right |//| <                | Less than                               | Left to right |//| >                | Greater than                            | Left to right |//| <=               | Less than or equal to                   | Left to right |//| >=               | Greater than or equal to                | Left to right |//| ==               | Equality                                | Left to right |//| !=               | Inequality                              | Left to right |//| &                | Bitwise AND                             | Left to right |//| ^                | Bitwise exclusive OR                    | Left to right |//| |                | Bitwise OR                              | Left to right |//| &&               | Logical AND                             | Left to right |//| ||               | Logical OR                              | Left to right |//| e1?e2:e3         | Conditional                             | Right to left |//| =                | Assignment                              | Right to left |//| *=               | Multiplication assignment               | Right to left |//| /=               | Division assignment                     | Right to left |//| %=               | Modulus assignment                      | Right to left |//| +=               | Addition assignment                     | Right to left |//| -=               | Subtraction assignment                  | Right to left |//| <<=              | Left-shift assignment                   | Right to left |//| >>=              | Right-shift assignment                  | Right to left |//| &=               | Bitwise AND assignment                  | Right to left |//| |=               | Bitwise inclusive OR assignment         | Right to left |//| ^=               | Bitwise exclusive OR assignment         | Right to left |//| ,                | Comma                                   | Left to right |//+------------------+-----------------------------------------+---------------+




0 0