• 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

    参考链接:

  • 相关阅读:
    javascript继承对象冒充
    javascript原型prototype(2)
    javascript继承call()和apply实现继承
    javascript继承原型链继承
    javascript原型prototype(3)
    没有宽高的情况下实现水平垂直居中
    TCP协议
    什么是模块化?模块化的好处是什么?
    数组中嵌套数组,转化为一个数组形式/二维数组转化为一维数组
    常见的请求头类型
  • 原文地址:https://www.cnblogs.com/linkworld/p/16380775.html
Copyright © 2020-2023  润新知