【翁恺】18-引用

本文详细介绍了C++中的引用,包括其与指针的相似性和区别。引用作为对象的别名,必须在声明时初始化且不能为NULL,而指针可以为空并可改变指向。此外,讨论了引用在内存位置、访问方式以及函数参数和返回值中的应用,强调了引用的不可变性和安全性。对比Java中单一的堆内存管理和访问方式,进一步阐述了C++引用的独特性。
摘要由CSDN通过智能技术生成

C++ 复杂在于:

  • 太多的能够放对象的地方:对象可以放在 堆栈、堆、全局数据区
  • 太多可以访问对象的方式:直接掌握对象(变量内放对象)、通过指针访问对象、引用去访问对象

3*3 共 9种组合。

Declaring references

  • references are a new data type in C++

    char c;            //a character
    char* p = &c;      //a pointer to a character
    char& r = c;       //a reference to a character,需要初始化
    

    r 是 c 的别名。

  • local or global variables 本地变量和全局变量

    • type& refname = name;
    • for ordinary varibles,the initial value is required
  • in parameter lists and member variables 参数表内或者作为成员变量可以没有初始化

    • type& refname
    • binding defined by caller or constructor

参数是调用函数时候给的。成员变量是构造对象的时候初始化。

references

  • declares a new name for an existing object

    int X = 47;
    int& Y = X;//Y is a reference to X,Y 是 X 的别名。
    
    //X and Y now refer to the same variable
    
    cout<<Y;//print 47
    Y = 18;
    cout<<x;//print 18

Rules of references 引用规则

  • references must be initialized when defined

  • initialization establishes a binding

    • in declaration

      int x = 3;
      int& y = x;
      const int&z = x;//类似于指针,通过 z 不能修改x, z是x的别名
    • as a function argument

      void f(int& x);
      f(y);	//initialized when function is called
  • bindings don't change at run time, unlike pointers

  • assignment changes the object referred-to

    int& y = x;
    y = 12;//changes value of x
  • the target of a reference must have a location!

    void func(int &);
    func(i*3);		//warning or error,i*3 有结果但是没有名字,不可以

一个函数的返回结果是引用,那么该函数可以做左值。 x 为 16。

一定要去看 g(x)的原型是什么,可能把a 给改了。

int g(int x){}

g(a)不知道调用哪个函数。不能重载。

pointers vs. references

  • references
    • can't be null
    • are dependent on an existing variable, they are an alias(别名) for an variable
    • can't change to a new "address" location
  • pointers
    • can be set to null
    • pointer is independent of existing objects
    • can be change to point to a different address

引用是const指针实现的,为了让代码简洁。

int* f(int* x){
    (*x)++;
    return x;	//safe,x is outside this scope
}

int& g(int& x){
    x++;	//same effect as in f()
    return x;	//safe,outside this scope
}

C++有三种方式放对象,有三种方式访问对象;Java只有一个地方可以放对象,所有对象都放在堆里面只有一种方式访问对象,就是通过“指针”因为它只能通过指针访问对象,因此他可以把那个“*”去掉,然后规定说这不叫指针这叫引用,这其实和C++的引用是不一样的,更像是C++的指针,因为引用是不能做引用之间的赋值的实际上是指针,区别在于外形上没有那个星号,不能做运算(C的指针可以做计算)

references实际上是一种指针,一种const的指针

restrictions

  • no references to references

  • no pointers to references

    int& * p;	//illegal, *p是引用。引用的地址是无法取到的
    • reference to pointer is ok

      void f(int* &p); // p是引用, 引用的对象是个指针

离变量最近的决定了p的基本类型。

  • no arrays of references

引用不是实体,所以没有引用的数组。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值