Why have pointer parameters? [duplicate]

廉价感情. 提交于 2019-12-23 10:52:57

问题


Possible Duplicates:
Why use pointers?
Passing a modifiable parameter to c++ function

Why would I want to have pointer parameters? The only reason I can see is for small functions to attempt to reduce confusion and a smaller memory footprint.


回答1:


In Passing a modifiable parameter to c++ function I answered when to use a reference instead of a pointer.

Conversely, prefer a pointer to a reference when any of the following are true:

  • It can be null
  • It can be changed (to point to something else)
  • It must be deleted

Some people also prefer a pointer when the thing that's being pointed at should be mutated, saying that the difference between a const reference and a non-const reference isn't obvious enough to the reader.




回答2:


  1. Compatibility with C code where references aren't available.
  2. Variable sized arrays.
  3. Option to have a missing parameter, i.e. NULL.



回答3:


Pointers are the only way to accept C-style strings and arrays. They're also the only way to share data between multiple threads (besides global objects, which... ew).




回答4:


There are many reasons to have pointers as parameters. A major reason is polymorphism. The pointer can point to a base class object or a subclass, and will call methods on that object accordingly.

I recommend Accelerated C++ by Koenig, he does a good job of explaining how polymorphism works in C++ via pointers (and references).




回答5:


Two reasons:

  1. A pointer on a 32-bit platform is only 4 bytes. If the data you're passing to the function is more than 4 bytes, you save some call time by passing the structure by reference instead of by value. (You may give up this performance advantage in indirection costs later, but that's where profiling comes in.)

  2. You can pass a non-const pointer to let the function modify the passed data.




回答6:


Other than the standard uses for a pointer parameter (that others have already stated), one reason that I can think of is to implement callbacks. You can have a function that accepts a function pointer as a parameter.




回答7:


I assume you mean as opposed to references? The thing that you can do with pointers is set them to NULL, which allows you to specify an uninitialized state. This can sometimes be useful, but the corollary to that is that if you want to enforce that NULL cannot be passed to your function then using a reference makes that clear.




回答8:


if you pass a simple variable to a function, the function creates a copy of that variable for use within the function's scope. When the function completes execution, any changes made to the passed simple variable won't be shown. This is because the variable itself was never altered within the function only a copy of it was. Passing a pointer to the variable to a function solves this problem.

int some_function(int num) {
   return num++;
}

int num1 = 15;

std::cout << some_function(num1) << std::endl; //prints 16

std::cout << num1 << std::endl; //prints 15



回答9:


IMO, pointers are made out in a lot of C++ documentation to be far more important than they actually are, whereas references are rarely discussed in introductory material (other than brief mentions in terms of parameters).

While pointers certainly have value (arrays, string, dynamic memory, etc.), a lot of their functionality can be implemented more simply with references. The only time I really use pointer parameters regularly is when I am using a third-party library which has such a use built into it.

Using references removes the copy-assignment step implicit in straight class parameters while not requiring tiresome pointer dereferencing (using * or ->) over and over again. In addition, learning to use references is important for applications such as operator overloading, which is vital for precision control of your code.




回答10:


Plenty of reasons, but references should be the "standard". Use pointers only if a reference won't do. Some pros and cons:

  • Pointers can have NULL values, something that's extremely useful
  • Pointers can be of generic types and be reinterpreted as other types (though don't you dare use void *)
  • Pointers can be used to pass huge arrays without a huge memory footprint

  • In the case of generic types, compiler optimizations can operate better with references, because the compiler has more information on used types

  • References arguably make code more readable, more in-line with java and c# and thus better readability by other types of developers

  • References do not actually pass the entire object, but rather a pointer to it, however more type information is available to the compiler.




回答11:


There is one more difference between pointer and references: -pointer can be a NULL-pointer -reference references always an real object [f(T &v); f(NULL); isn't really good idea]



来源:https://stackoverflow.com/questions/3064976/why-have-pointer-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!