📜  在C++中使用带有引用指针的Const的不同方法

📅  最后修改于: 2021-05-30 17:28:57             🧑  作者: Mango

在继续将const与指向指针一起使用之前,让我们首先了解它们是什么:

  • 指针用于存储变量的地址或存储位置。通过在声明中添加“ * ”,可以将变量声明为指针。
    datatype *var_name; 
    

    例子:

    // C++ program to
    // demonstrate a Pointer
      
    #include 
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
      
        // Pointer to i
        int* ptr_i = &i;
      
        cout << *ptr_i;
      
        return 0;
    }
    
    输出:
    10
    
  • 引用:将变量声明为引用时,它将成为现有变量的替代名称。通过在声明中添加“& ”,可以将变量声明为引用。
    datatype &var_name; 
    

    例子:

    // C++ program to
    // demonstrate a Reference
      
    #include 
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
      
        // Reference to i.
        int& ref = i;
      
        // Value of i is now
        // changed to 20
        ref = 20;
      
        cout << i;
      
        return 0;
    }
    
    输出:
    20
    
  • 对指针的引用是一个可修改的值,其用法与普通指针相同。
    datatype *&var_name; 
    

    范例1:

    // C++ program to demonstrate
    // References to pointers
      
    #include 
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
      
        // Pointer to i
        int* ptr_i = &i;
      
        // Reference to a Pointer ptr_i
        int*& ptr_ref = ptr_i;
      
        cout << *ptr_ref;
      
        return 0;
    }
    
    输出:
    10
    

    这里ptr_ref指向指针ptr_i的引用,该指针指向变量’i’。因此,在ptr_ref上的打印值将给出’i’的值,该值为10。

    示例2:现在让我们尝试更改由指针引用表示的地址

    // C++ program to demonstrate
    // References to pointers
      
    #include 
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
        int j = 5;
      
        // Pointer to i
        int* ptr = &i;
      
        // Reference to a Pointer ptr
        int*& ptr_ref = ptr;
      
        // Trying to change the reference
        // to Pointer ptr_ref to address of j
        ptr_ref = &j;
      
        cout << *ptr;
      
        return 0;
    }
    
    输出:
    5
    

    在这里它打印5,因为j的值为5,并且我们将ptr_ref更改为指向j。现在,由于ptr_ref是对指针ptr的引用,因此ptr现在指向j。这样我们得到了我们期望看到的输出。

  • const对指针的引用是一个不可修改的值,其用法与const指针相同。
    datatype* const &var_name; 
    

    范例1:

    // C++ program to demonstrate
    // References to pointers
      
    #include 
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
        int j = 5;
      
        // Pointer to i
        int* ptr = &i;
      
        // Const Reference to a Pointer
        int* const& ptr_ref = ptr;
      
        // Trying to change the const reference
        // to Pointer ptr_ref to address of j
        ptr_ref = &j;
      
        cout << *ptr;
      
        return 0;
    }
    

    编译错误:

    In function 'int main()':
    prog.cpp:23:13: error: assignment of read-only reference 'ptr_ref'
         ptr_ref = &j;
                 ^
    

    在这里,我们得到一个编译时错误,因为它是对指针的const引用,因此我们不允许重新分配它。

    范例2:

    // C++ program to demonstrate
    // References to pointers
      
    #include 
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
        int j = 5;
      
        // Pointer to i
        int* ptr = &i;
      
        // Const Reference to a Pointer
        int* const& ptr_ref = ptr;
      
        // Trying to change the reference
        // to Pointer ptr_ref
        *ptr_ref = 100;
      
        cout << *ptr;
      
        return 0;
    }
    
    输出:
    100
    

    它打印100,因为它不是对const指针的引用。

    为什么示例2出现示例2时没有引发编译时错误?

    • 在示例1中ptr_ref是对指向int的指针的const引用,我们试图更改ptr_ref的值。因此,当我们尝试修改常量值时,编译器将引发编译时错误。
    • 在示例2中ptr_ref是指向int的指针的const引用,我们试图更改* ptr_ref的值,这意味着我们正在更改指针指向的int的值,而不是指向const的引用。一个指针。因此,编译器不会引发任何错误,并且指针现在指向值100。因此,这里的int不是常量,而是指针。结果,int的值更改为100。
  • 对常量指针的引用是对常量指针的引用。
    datatype const *&var_name; 
    

    例子:

    // C++ program to demonstrate
    // References to pointers
      
    #include 
    using namespace std;
      
    int main()
    {
      
        // Variable
        int i = 10;
        int j = 5;
      
        // Const Pointer to i
        int const* ptr = &i;
      
        // Reference to a Const Pointer
        int const*& ptr_ref = ptr;
      
        // Trying to change the value of the pointer
        // ptr with help of its reference ptr_ref
        *ptr_ref = 124;
      
        cout << *ptr;
        return 0;
    }
    

    编译错误:

    In function 'int main()':
    prog.cpp:23:14: error: assignment of read-only location '* ptr_ref'
         *ptr_ref = 124;
                  ^
    

    在这里,我们再次得到编译时错误。这是因为在这里编译器说要声明ptr_ref作为对const int指针的引用。因此,我们不允许更改i的值。

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”