C ++ Primer: reference, pointer and const usage summary


1. Background

  When looking cited Chapter II "C ++ Primer", pointers, and const, found some confused look, now sum up the references, and the use of const pointer.

2 references

  1. Object reference not just an alias.
  2. Reference must be initialized when defined, and can only bind a target.
  3. Reference only binding object, the results can not bind a literal or expression. Type of reference type must be associated with it exactly matches the object (with two exceptions, a reference constant and a reference to the base class).
#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    double d = 3.14;

    //引用必须在定义时初始化
    int &r1 = i;
    int &r2; // r2未初始化

    //引用只能绑定对象(左值),不能绑定字面值或表达式的计算结果(右值)
    int &x1 = i;
    int &x2 = 1; // 错误,引用不能绑定字面值
    int &x3 = 3 + i; //错误,引用不能绑定计算结果

    //引用的类型必须与其所绑定对象的类型匹配
    int &y1 = i;
    int &y2 = d;//错误,类型不匹配,y2绑定int,而d是double

    //引用即别名
    r1 = 10;//i1=10

    return 0;
}

3. Pointer

  1. Object pointers may point to different objects, you may not be defined with an initial value.
  2. Pointer type to be consistent with the type of object it points (with two exceptions, and a pointer to a pointer to the base class constants).
  3. void * pointer can store any type of pointer, but not the operation target to which it points.
#include <iostream>
using namespace std;

int main()
{
    int i1 = 1, i2 = 2;
    double d = 3.14;

    // 指针可以不在定义时初始化
    int *p1;

    // 指针可以指向不同对象
    p1 = &i1;
    p1 = &i2;

    //指针的类型必须与其所指对象的类型一致
    int *p2 = &i1;
    int *p3 = &d; // 错误,类型不匹配,p3指向int,d是double

    // void*可以存放任意类型的指针,但无法操作其指向的对象
    void *p4 = &i1;
    p4 = &d;
    cout << p4 << endl;
    cout << *p4 << endl;// 错误,p4是void *,无法操作其所指的对象

    return 0;
}

4. const

  1. const must be initialized when defined and can not be modified.
  2. Specific examples cited: long as the expression can be converted into the type of reference, references to it allows constant as an initial value.
  3. Specific examples pointers: pointing to a constant pointer can point to a corresponding non-constant object.
#include <iostream>
using namespace std;

int main()
{
    int i = 0;
    double d = 3.14;

    // const必须在定义时初始化
    const int ci = 3;

    // const对象不可修改
    ci = z1;

    //对常量的引用
    const int &r1 = ci;

    //引用特例:若表达式能转换成引用类型,则对常量的引用允许以它作为初始值
    const int &r2 = 30; //30是字面值,可类型转换成const int
    //等价于
    const int temp2 = 30; //类型转换
    const int &r2 = temp2;

    const int &r3 = d; //d可从double转换成const int
    //等价于
    const int temp3 = d; //类型转换
    const int &r3 = temp3;

    int &r4 = d; //假设表达式正确,则可以通过修改r4来修改d
    //等价于
    int temp4 = d;   //r4指向int,d是double,需先进行类型转换
    int &r4 = temp4; //此时,r4绑定temp,而通过修改r4只能修改temp4,无法修改d。结果与假设矛盾

    //指向常量的指针
    const int *p1 = &ci;
    //指针特例:指向常量的指针可以指向一个相应的非常量对象,
    const int *p2 = &i; // 正确,特例,     p2指向const int,i是int
    const int *p3 = &d; // 错误,类型不匹配,p3指向const int,d是double

    //本身是常量的指针
    int *const x1 = &i;
    int *const x2 = &ci; // 错误,类型不匹配,x2指向int,ci是const int
    int *const x3 = &d;  // 错误,类型不匹配,x3指向int,d是double

    const int *const y1 = &ci;
    const int *const y2 = &i; // 正确,指针特例:y2指向const int,i是int
    const int *const y3 = &d; // 错误,类型不匹配,y3指向const int,d是double

    return 0;
}
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/103884905