是否解引用指针使其成为副本? [英] Does dereferencing a pointer make a copy of it?

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

问题描述

解引用一个指针并将其传递给一个函数,它通过引用创建一个对象的副本。

解决方案

在这种情况下,指针的值被复制(虽然这不一定是优化器可以优化它的情况)。

  int val = * pPtr;在这种情况下,不会发生复制:

  int& rVal = * pPtr; 

没有复制发生的原因是因为引用不是机器码级别构造。它是一个更高级别的结构,因此是编译器在内部使用,而不是为它生成特定的代码。



同样,显然,函数参数。


Does dereferencing a pointer and passing that to a function which takes its argument by reference create a copy of the object?

解决方案

In this case the value at the pointer is copied (though this is not necessarily the case as the optimiser may optimise it out).

int val = *pPtr;

In this case however no copy will take place:

int& rVal = *pPtr;

The reason no copy takes place is because a reference is not a machine code level construct. It is a higher level construct and thus is something the compiler uses internally rather than generating specific code for it.

The same, obviously, goes for function parameters.

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

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