• C++ 引用


    10. 引用

    • References are a new data type in C++;
    • Local or global variables
      • For ordinary variables, the initial value is required
    • In parameter lists and member variables
      • Binding defined by caller or constructor
    char c;         // a character
    char* p = &c;   // a pointer to a character
    char& r = c;    // a reference to a character
    

    10.1 引用详解

    • 引用就是别名
      • Declares a new name for an existing object
    int X = 47;
    int& Y = X;    // Y is a reference to X
    
    // X and Y now refer to the same variable
    cout << "Y = " << Y << endl;
    Y = 18;
    cout << "X = " << X << endl;
    

    10.2 引用的规则

    • References must be initialized when defined
    • Initialization establishes a binding
    // In declaration
    int x = 3;
    int& y = x;
    const int& z = x;
    
    // As a function argument
    void f(int& x);
    f(y);       // initialized when function is called
    

    10.3 示例

    #include <iostream>
    using namespace std;
    
    int* f(int* x) {
        (*x)++;
        return x;
    }
    
    int& g(int& x) {
        x++;
        return x;
    }
    
    int x;
    int& h() {
        int q;
        return x;
    }
    
    int main() {
        int a = 0;
        f(&a);  // Ugly (but explicit)
        cout << "f(&a) = " << a << endl;
        g(a);   // Clean (but hidden)
        cout << "g(a) = " << a << endl;
        h() = 16;
        cout << "h() = " << x << endl;
    }
    

    10.4 Pointers vs. References

    • Pointers

      • can be set to null
      • pointer is independent of existing objects
      • can change to point to a different address
    • 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
    • 约束

    // No references to references
    
    // No pointers to references
        int&* p;        // illegal
    
    // Reference to pointer is ok
        void f(int*& p);    // ok
    
    // No arrays of references
    

    10.5 References as class members

    • Declared without initial value
    • Must be initialized using constructor initializer list
    class X {
    public:
        int& m_y;
        X(int& a);
    };
    
    X::X(int& a) : m_y(a) { }
    

    10.6 Returning references

    • Functions can return references
      • But they better refer to non-local variables!
    #include <assert.h>
    const int SIZE = 32;
    double myarray[SIZE];
    
    double& subscript(const int i) {
        return myarray[i];
    }
    

    10.7 const in Functions Arguments

    • Pass by const value -- don't do it
    • Passing by const reference: Person (const string& name, int weight);
      • don't change the string object
      • more efficient to pass by reference(address) than to pass by value(copy)
      • const qualifier protects from change

    10.8 Temporary values are const

    • 示例:
    #include <iostream>
    using namespace std;
    
    void f(cosnt int& i)
    {
        cout << i << endl;
    }
    
    int main()
    {
        int i = 3;
        f(i * 3);
    
        return 0;
    }
    
    • 错误示例:
    void func(int&);
    func(i * 3);    // Generates error!
    
    // 实际编译结果:
    void func(int&);
    const int temp@ = i * 3;
    func(temp@);  // Problem -- binding const ref to non-const argument!
    
    // The temporary is constant, since you can't access it.
    

    10.9 const in Function returns

    • return by const value
      • For user defined types, it means "prevent use as an lvalue"
      • for built-in's it means nothing
    • return by const pointer or reference
      • depends on what you want your client to do with the return value

    参考链接:

  • 相关阅读:
    对称加密和非对称加密
    SpringMVC 请求调用过程
    Swagger2常用注解和使用方法
    玩转汉诺塔
    java序列化
    判断当前时间是否在某时间段内
    docker限制容器日志大小
    MySQL数据库备份与恢复
    MySQL事物
    MySQL用户
  • 原文地址:https://www.cnblogs.com/linkworld/p/16380775.html
Copyright © 2020-2023  润新知