zoukankan      html  css  js  c++  java
  • The differences between pointer and reference in C++

    I'm learning C++ from LearnCpp, you should read it before this article. There is an example just like this: [c language="++"] class Calc { private: int m_nValue; public: Calc() { m_nValue = 0; } Calc& Add(int nValue) { m_nValue += nValue; return *this; } Calc& Sub(int nValue) { m_nValue -= nValue; return *this; } Calc& Mult(int nValue) { m_nValue *= nValue; return *this; } int GetValue() { return m_nValue; } }; int main() { using namespace std; Calc cCalc; cCalc.Add(5).Sub(3).Mult(4); cout<<cCalc.GetValue()<<endl; return 0; } [/c] In the line 7,8,9 reference is used ,It's evoked in line 18. If we want to use pointer to realize same result,The program should be like this: [c language="++"] class Calc { private: int m_nValue; public: Calc() { m_nValue = 0; } Calc * Add(int nValue) {//return type should be pointer of object instead of reference m_nValue += nValue; return this;//return pointer of object instead of object } Calc& Sub(int nValue) { m_nValue -= nValue; return *this; } Calc& Mult(int nValue) { m_nValue *= nValue; return *this; } int GetValue() { return m_nValue; } }; int main() { Calc cCalc; cCalc.Add(5)->Sub(3).Mult(4);//pointer should be used as this cout << cCalc.GetValue() << endl; return 0; } [/c] The differences as listed below:
    1. A pointer can be re-assigned:[c]int x = 5; int y = 6; int *p; p = &x; p = &y; *p = 10; assert(x == 5); assert(y == 10);[/c] A reference cannot, and must be assigned at initialization: [c]int x = 5; int y = 6; int &r = x;[/c]
    2. A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack. Since a reference has the same address as the original variable itself, it is safe to think of a reference as another name for the same variable. Note: What a pointer points to can be on the stack or heap. Ditto a reference. My claim in this statement is not that a pointer must point to the stack. A pointer is just a variable that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references. More on stack vs heap. This implies that there is a real address of a reference that the compiler will not tell you. [c]int x = 0; int &r = x; int *p = &x; int *p2 = &r; assert(p == p2);[/c]
    3. You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection. [c]int x = 0; int y = 0; int *p = &x; int *q = &y; int **pp = &p; pp = &q;//*pp = q **pp = 4; assert(y == 4); assert(x == 0);[/c]
    4. Pointer can be assigned NULL directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference NULL. Likewise, if you try hard enough you can have a reference to a pointer, and then that reference can contain NULL. [c]int &r = NULL;// <--- compiling error[/c]
    5. Pointers can iterate over an array, you can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element. This is no matter what size the object is that the pointer points to.
    6. A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access it's members whereas a reference uses a ..
    7. A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address as the item it references.
    8. References cannot be stuffed into an array, whereas pointers can be (Mentioned by user @litb)
    9. Const references can be bound to temporaries. Pointers cannot (not without some indirection):
    10. [c]const int &x = int(12); //legal C++ int *y = &amp;int(12); //illegal to dereference a temporary. [/c]This makesconst & safer for use in argument lists and so forth.
  • 相关阅读:
    python自动化测试,将测试结果的报告写入本地中(HTMLTestRunner)
    谷歌+selenuim ide导出python代码 详细代码
    谷歌+selenium插件的安装
    C# List转DataTable(支持匿名类型)
    喵的Unity游戏开发之路
    喵的Unity游戏开发之路
    喵的Unity游戏开发之路
    喵的Unity游戏开发之路
    喵的Unity游戏开发之路
    Unity3D游戏开发入门引导:Unity3D收费方案和版本、下载地址、安装教程
  • 原文地址:https://www.cnblogs.com/benhuan/p/3302108.html
Copyright © 2011-2022 走看看