What is the point of pointers?

后端 未结 12 1334
梦谈多话
梦谈多话 2021-02-01 18:49

What is the point of pointers in C++ when I can just declare variables? When is it appropriate to use them?

12条回答
  •  长情又很酷
    2021-02-01 19:47

    Pointers are best understood by C & C++'s differences in variable passing to functions.

    Yes, you can pass either an entire variable or just a pointer to it (jargon is by value or reference, respectively).

    But what if the variable is 20 meg array of bytes, like you decided to read an entire file in to one array? Passing it by value would be foolish: why would you copy 20 megs for this operation, and if you end up modifying it (i.e. it's an out-parameter) you have to copy that 20 megs BACK?

    Better is to just "point" to it. You say, "here's a pointer to a big blob of memory". And that little indirection saves a ton of time.

    Once you understand that, everything else is basically the same. Rearranging items in a list becomes just swapping pointers rather than copying every item around, you don't need to know how big things are when you start out, etc

提交回复
热议问题