我在这里得到结构副本吗?
这是一个假代码示例
vector<Fred> gFred;
{
// init gFred
Fred &fred = gFred[0];
size_t z = 0;
do
{
fred = gFred[z];
// do odd processing with fred
z++;
}
while (fred.lastElementInSet == 0);
}
引起我注意的是 gFred[0] 被覆盖。 这让我想到,
fred = gFred[z];
实际发生的情况是 gFred[1] 正在覆盖 gFred[0],而不是 init fred 作为对新元素的引用。
我认为在这里要做的正确的事情就是敲打自己的头几次,然后将其转变为指针实现并继续我的生活。
我的诊断正确吗? 或者我需要更多的教育吗?
Here is a fake code sample
vector<Fred> gFred;
{
// init gFred
Fred &fred = gFred[0];
size_t z = 0;
do
{
fred = gFred[z];
// do odd processing with fred
z++;
}
while (fred.lastElementInSet == 0);
}
The thing that caught my attention was the fact that gFred[0] was being overwritten.
This leads me to think that rather than init fred as a reference to a new element,
fred = gFred[z];
What is actually happening is that gFred[1] is overwriting gFred[0].
I'm thinking the correct thing to do here, is whap myself upsida head a few times, and turn this into a pointer implementation and move on with my life.
Have I diagnosed this correctly? or do I need more education?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您将在那里获得结构副本。 引用不能反弹,即一旦初始化,它们就保持不变。
而且你的解决方案也是合适的。 不过不知道要不要敲自己的头。
Yes, you are getting a structure copy there. References cannot be rebound, i.e., they stay the same once they are initialized.
And your solution is also appropriate. Dunno about smacking yourself in the head though.
查看引用的一种方法是将它们视为隐式解除引用的指针。 简而言之,它们是指针,但您可以使用普通的变量访问语法来使用它们。
这将创建对向量 gFred 第一个元素的引用。 (顺便说一句,你里面有什么东西吗?)编译器会做这样的事情:
现在,当你这样做时:
编译器实际上会做这样的事情:
翻译后的意思是:
你正在做这个
N 次,如果您的
向量
中有 N 个元素。如果您尝试初始化
vector
的所有元素,请尝试使用此构造函数:其中,
One way of looking at references is to think of them as implicitly de-referenced pointers. Simply put, they are pointers, but you can use the normal variable access syntax to use them
This creates a reference to the first element of your vector gFred. (Incidentally, do you have anything inside this?) The compiler will do something like this:
Now, when you do:
the compiler will actually do something like this:
which translated stands as:
And you are doing this
N
times, if you have N elements to start off in yourvector
.If you are trying to initialize all elements of your
vector
try this constructor:where,
使用您发布的代码(假设 Fred 类型是 POD),gFred[0] 将被重写,并且最终将包含最后一个 z 的 gFred[z] 中的 wahatever 的副本。
您可以切换到使用指针实现,或者可以更仔细地限定引用范围:
With the code you posted (and assuming the Fred type is a POD), gFred[0] is being written over and will end up containing a copy of wahatever was in gFred[z] for the last z.
You could switch to using a pointer implementation, or you could scope the reference more closely:
在您提供的代码中,
fred
始终引用gFred[0]
。 如果您希望fred
引用随着循环的每次迭代而更改,请删除行Fred &fred=gFred[0];
。 然后将fred = gFred[z];
替换为Fred &fred = gFred[z]
。 这样,您就可以在每次循环执行时重新初始化fred
引用。In the code you give,
fred
is always referencinggFred[0]
. If you want thefred
reference to change with each iteration of the loop, remove the lineFred &fred=gFred[0];
. Then replacefred = gFred[z];
withFred &fred = gFred[z]
. This way, you reinitialize thefred
reference each time the loop executes.编译机制由dirkgently的答案解释,而底层解释是MSN的答案(“引用不能反弹”)。
也许这样读起来更容易:
if
表示
ptr
的内容是xxx
的地址,then表示
的地址 >fred
设置为指向gFred[0]
。 IOW,fred
现在是gFred[0]
的别名。 这就是为什么fred = xxx
会覆盖gFred[0]
the compiled mechanism is explained by dirkgently's answer, while the underlying explanation is MSN's one ("References cannot be rebound").
maybe it's easier to read this way:
if
means that the content of
ptr
is the address ofxxx
, thenmeans that the address of
fred
is set to point togFred[0]
. IOW,fred
is now an alias togFred[0]
. that's why doingfred = xxx
overwritesgFred[0]