我在这里得到结构副本吗?

发布于 2024-07-14 09:53:44 字数 516 浏览 12 评论 0原文

这是一个假代码示例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里泪两行 2024-07-21 09:53:44

是的,您将在那里获得结构副本。 引用不能反弹,即一旦初始化,它们就保持不变。

而且你的解决方案也是合适的。 不过不知道要不要敲自己的头。

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.

烦人精 2024-07-21 09:53:44

查看引用的一种方法是将它们视为隐式解除引用的指针。 简而言之,它们是指针,但您可以使用普通的变量访问语法来使用它们。

Fred &fred = gFred[0];

这将创建对向量 gFred 第一个元素的引用。 (顺便说一句,你里面有什么东西吗?)编译器会做这样的事情:

Fred *pFred = &gFred[0];

现在,当你这样做时:

fred = gFred[z];

编译器实际上会做这样的事情:

*pFred = gFred[z];

翻译后的意思是:

gFred[0] = gFred[z];

你正在做这个N 次,如果您的向量中有 N 个元素。

如果您尝试初始化 vector 的所有元素,请尝试使用此构造函数:

vector(size_type n, const T& t)      

其中,

n = size of vector
t = gFred[0]

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

Fred &fred = gFred[0];

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:

Fred *pFred = &gFred[0];

Now, when you do:

fred = gFred[z];

the compiler will actually do something like this:

*pFred = gFred[z];

which translated stands as:

gFred[0] = gFred[z];

And you are doing this N times, if you have N elements to start off in your vector.

If you are trying to initialize all elements of your vector try this constructor:

vector(size_type n, const T& t)      

where,

n = size of vector
t = gFred[0]
青衫负雪 2024-07-21 09:53:44

使用您发布的代码(假设 Fred 类型是 POD),gFred[0] 将被重写,并且最终将包含最后一个 z 的 gFred[z] 中的 wahatever 的副本。

您可以切换到使用指针实现,或者可以更仔细地限定引用范围:

{    
    size_t z = 0;
    do
    {
        Fred &fred = gFred[z];
        // do odd processing with fred
        z++;
    }
    while (fred.lastElementInSet == 0);
}

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:

{    
    size_t z = 0;
    do
    {
        Fred &fred = gFred[z];
        // do odd processing with fred
        z++;
    }
    while (fred.lastElementInSet == 0);
}
木有鱼丸 2024-07-21 09:53:44

在您提供的代码中,fred 始终引用 gFred[0]。 如果您希望 fred 引用随着循环的每次迭代而更改,请删除行 Fred &fred=gFred[0]; 。 然后将 fred = gFred[z]; 替换为 Fred &fred = gFred[z]。 这样,您就可以在每次循环执行时重新初始化 fred 引用。

In the code you give, fred is always referencing gFred[0]. If you want the fred reference to change with each iteration of the loop, remove the line Fred &fred=gFred[0]; . Then replace fred = gFred[z]; with Fred &fred = gFred[z]. This way, you reinitialize the fred reference each time the loop executes.

请帮我爱他 2024-07-21 09:53:44

编译机制由dirkgently的答案解释,而底层解释是MSN的答案(“引用不能反弹”)。

也许这样读起来更容易:

if

ptr = &xxx;

表示ptr的内容是xxx的地址,then

&fred = gFred[0];

表示地址 >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

ptr = &xxx;

means that the content of ptr is the address of xxx, then

&fred = gFred[0];

means that the address of fred is set to point to gFred[0]. IOW, fred is now an alias to gFred[0]. that's why doing fred = xxx overwrites gFred[0]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文