0
点赞
收藏
分享

微信扫一扫

cpp_引用和指针_理解的记录

大柚子top 2022-03-12 阅读 56
c++
#include <iostream>
using namespace std;
class tester
{
private:
    int value;

public:
    tester() : value(5){
        cout << "tester at " << this << " created" << endl;
    }
    int get(){
        return value;
    }
    ~tester(){
        cout << "tester at " << this << " deleted" << endl;
    }
};
int main()
{
    tester &ref1 = *new tester();
    tester &ref2 = ref1;

    cout << ref2.get() << endl;
    delete &ref1;
    cout << ref2.get() << endl;
}

tester at 0xe618f0 created
5
tester at 0xe618f0 deleted
15106864

理解上,
 引用 是 指向一个对象 的指针
  不能指向空,不能指向其它对象
所以课程讲解时,
 引用 是 对象的别名

举报

相关推荐

0 条评论