C++ 笔记(15)— 引用(声明引用、引用作为参数、引用作为函数返回值、const 用于引用)
发布日期:2021-05-10 08:43:56 浏览次数:18 分类:精选文章

本文共 4363 字,大约阅读时间需要 14 分钟。

���������References in C++���

��������������������������������������������������������������������������������������������������������������������������������������������������� C++ ��������������������������������������������������������������������������������������������������������������������������������������������������������������� C++ ���������������������������


1. ������������

������������������������������������������������������������������������������������������������������������������������������������������������ i ������������������������ d���������������������������������������������������������

int i = 0;
double d = 100.5;
int& r = i; // r ��� i ������ ���
double& s = d; // s ��� d ������ ���

������������������r ��� s ������ i ��� d ������������������������������������������������������r ������������������������������ s ���������������������������

������������������������ & ������������������������������

int& r = i; // ��������� & ������ r ��� i ������ ���
double& s = d; // s ��� d ������ ���

2. ������������������������

���������������������������������������������������������������������������������������������

  • ������������������

    ������������������������������������������������������������������������������������������������������������������������������������������

    void* ptr = nullptr; // ��������������������� null
    int& ref; // ������ ref ��������������� null
  • ���������������������������

    ������������������������������������������������������������������������������������������������������������������������������������������ null���

  • ���������������������������������

    ������������������������������������������������������������������������������������������

    int& r;  // ��������� r ������������������������ ���
    r = 5; // ������������������������������������ ��� ���������������
  • ������������������������������������������������������������������


    3. ���������������

    3.1 ���������������������������������

    ���������������������������������������������������������������������������������������������������������������������������

    void DoSomething(Type& parameter); // ���������������

    ������������������������������

    ReturnType Result = DoSomething(argument); // argument ��������� ������������

    ��������������� DoSomething ������������������������������������������������������������ argument ���������

    3.2 ������������������������������

    ������������������������������������������������������������������������������������

    void getSequare(int& num) {
    num *= num;
    }

    ������ getSequare ��������������������� ��� num������������������������������������������������������������������������ num ������ ��������������������������������������������������� num ������������������������������������������


    4. ������ const ���������

    ������������������������ ������������������������������������������������������������������������ const ���������������������

    int original = 30;
    const int& constRef = original; // constRef ������������ original ������
    // ������������������������
    constRef = 40;
    int& ref2 = constRef; // ref2 ������������������

    5. ���������������������������

    ������������������������������������������������������������������������������������

    int& getValue(); // ��������������������������� ���
    int main() {
    int& a = getValue();
    cout << "a is " << a << endl; // ��� a ������������������
    return 0;
    }

    ������ getValue ��������������������������������������������������������� ���������������

    int& getValue() {
    static int ret = 100;
    return ret;
    }

    ������������������������������������������������������������������������������������


    6. ���������������������������������

    6.1 ������������������������������

    -������������ . ������������������������������������������������������

    obj.func();
    obj.data;

    -������������ -> ���������������������

    PointerClass* p = new PointerClass();
    p->func();
    p->data;

    6.2 ������������������������

    ��������������������������������� NULL ������������ C++ ��������������������������������������������������������� ��������� ���������������������������

    6.3 ������������

    • ���������������������������������������������
    • ��������������� const ��������������������� volatile ������������������������������������������������������������������������������������

    ��������������������������������������� C++ ���������������������������������������������������������������

    上一篇:C++ 笔记(17)— 类和对象(构造函数、析构函数)
    下一篇:C++ 笔记(13)— 函数(函数声明、函数定义、函数调用[传值、指针、引用]、函数参数默认值、函数重载)

    发表评论

    最新留言

    很好
    [***.229.124.182]2025年04月14日 21时17分55秒