什么是引用指针? [英] What is a reference-to-pointer?

查看:152
本文介绍了什么是引用指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看到一个函数被声明为:

  void func(type *& param); 

我已经知道 type * param type& param 。上述与他们有什么不同?和什么时候使用这个?

解决方案

上面的命令不仅可以修改指向的对象,还可以修改指针本身。例如,考虑下面的代码:

  void func(int *& ptr){
* ptr = 1;
ptr = 0;
}

int main(){
int x = 0;
int * y =& x;
func(y);
}

执行结束时, code>有值 1 y 0 (如您可以看到)。



请注意,为了示例的目的,我使用 0 作为空指针,但如果您使用C ++ 11,则应该可能使用 nullptr 而不是(对于没有重载的操作数< std :: ostream )。






这个概念可以通过查看下面的代码来吸收:

 模板< class Type>使用ptr = Type *; 
ptr< int>& X;

  std :: unique_ptr< int>& X; 

在这些示例中, x 到一个类型( ptr ,然后 std :: unique_ptr< int> 是具有指针语义( operator * operator-> )的指针/类。






可以对指针中的 const 限定符的位置进行一个可能有趣的挖掘。考虑这两个实例:


  1. void func(const int *& ptr)

  2. void func(int * const& ptr)


b $ b

它们的含义是:

通过引用常量int


  1. >
  2. 指针通过常量引用int

按照上述与 ptr 的比较,它们将是:


  1. ptr< const int>&

  2. ptr< int> const&

因此,第一个将无法执行 * ptr = / code>在函数体中(因为 int 是常量),但会很乐意执行 ptr = 0



第二个行为相反,允许 * ptr = 1 (因为指向的int不是常量)不允许 ptr = 0 (因为指针是常量)。



当然, p>

  void func(const int * const& ptr)

其中在 ptr 类比中将是 ptr< const int>







和什么时候使用这个?是否建议这样做?


像每个功能一样,当你需要它时,你会发现它的有用性。但是作为一个一般的想法,有些人使用它来释放动态分配的资源后重置指针(我不推荐这个,见下文)。



此示例:

  free_my_int(int *& ptr){
delete ptr;
ptr = nullptr;
}

int * x = new int(42);
free_my_int(x);

执行结束时, x 将被正确释放,指针自动设置为 nullptr (空指针)。这是为了防止由于缺少 ptr = nullptr 造成的丑陋分段错误或未分配指针释放错误消息。

但是使用C ++ 11和C ++ 14,很少使用指针,甚至少用对指针的引用。用于的大多数事物指针不会替换为其他标准构造(请参阅 std :: optional std :: unique_ptr std :: shared_ptr std :: reference_wrapper )。


I recently saw a function that is being declared as:

void func(type* &param);

I already know the difference between type* param and type& param. How does the above differ from them? And when to use this? Is it advisable to do this?

解决方案

The above will be able to modify not only the pointed object, but also the pointer in itself. As an example, consider the below code:

void func(int*& ptr) {
    *ptr = 1;
    ptr = 0;
}

int main() {
    int x = 0;
    int* y = &x;
    func(y);
}

At the end of the execution, x has value 1 and y is 0 (as you can see).

Notice that for the sake of the example I've used 0 as a null pointer, but if you are using C++11 you should probably use nullptr instead (which does not have an overloaded operaror<< for std::ostream).


This concept can possibly be assimilated by taking a look at the following code:

template<class Type> using ptr = Type*;
ptr<int>& x;

or

std::unique_ptr<int>& x;

In these examples, x is a reference to a type (ptr<int> and then std::unique_ptr<int>), which just so happens to be a pointer/class with pointer semantic (operator* and operator->).


A possibly interesting digression can be made on the position of a const qualifier in the pointer. Consider these two instances:

  1. void func(const int*& ptr)
  2. void func(int*const& ptr)

Their meanings are:

  1. pointer by reference to a constant int.
  2. pointer by constant reference to an int.

And following the above analogy with ptr they would be:

  1. ptr<const int>&
  2. ptr<int> const&

Therefore the first will fail to execute *ptr = 1 in the body of the function (because the int is constant), but will happily execute ptr = 0.

The second will behave conversely, allowing *ptr = 1 (because the pointed int is not constant), while disallowing ptr = 0 (because the pointer is constant).

Of course, in the case of:

void func(const int*const& ptr)

which in the ptr analogy would be ptr<const int> const&, both of them wouldn't be allowed.


And when to use this? Is it advisable to do this?

Like every feature, you'll find its usefulness when you'll need it. But just as a general idea, some people used it to reset the pointer after freeing a dynamically allocated resource (I'm not recommending this, see below).

Let's take this example:

free_my_int(int*& ptr) {
    delete ptr;
    ptr = nullptr;
}

int* x = new int(42);
free_my_int(x);

At the end of the execution, x would be correctly freed and the pointer automatically set to nullptr (null pointer). This was done to prevent ugly segmentation faults or "pointer freed has not been allocated" error messages caused by a missing ptr = nullptr.

But with C++11 and C++14 there is very little use of pointers and even less of reference to pointers. Most of the things pointers where used for are not replaced with other standard construct (see std::optional, std::unique_ptr, std::shared_ptr or std::reference_wrapper for example).

这篇关于什么是引用指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆