【发布时间】:2018-08-26 20:03:25
【问题描述】:
也许这个问题会有点复杂,也许我错过了一些愚蠢的东西。 我将尝试在没有任何源代码的情况下进行解释,因为我的项目很大,我不知道如何/从哪里开始。 我有:
bool result = false;
bool* pointer = &result;
这些变量存储在某些类中..(与上面的代码不同)。
创建result 时,他的地址类似于0x28fddc。
并且指针变量采用这个地址。
突然,没有任何理由(也许),他的地址不再是 0x28fddc,而是类似于0x3a8c6e4。
使用指针变量,我试图通过以下方式更改结果变量:
*result = true;
但显然,这不起作用(并且它不会给我任何错误)。它不会改变结果变量,因为它在另一个地址中。 我不知道为什么会这样。 你能告诉我这是怎么发生的吗?我会尝试修复。 (这些类每次都在某些函数中使用通过引用传递的参数进行更新)。 例如:
void update_class(obj_class &obj);
(这些名称只是一个示例)。 我希望我已经清楚了,如果没有,我会删除这个话题。 抱歉英语不好,但我是意大利人:)
编辑: 现在我将尝试提供一些代码..
按钮.h
class button
{
public:
void check_tap(SDL_Event* e);
bool* done;
}
messagebox.h:
class messagebox
{
public:
messagebox();
bool result_ok;
button btn_ok;
}
void check_tap(std::vector<messagebox> &msgbox, SDL_Event* e) {
for(unsigned int k=0; k<msgbox.size(); k++) {
msgbox[k].btn_ok.check_tap(e);
// check_tap is a function that i create for checking if the user is tapping the button with his finger or not. When the user presses the button and leaves it the done variable should become true, but result_ok seems not to be affected because his address here is different. This problem is only in this case using messagebox. I created more other buttons outside and all works perfect.
}
}
messagebox.cpp:
messagebox::messagebox() {
// Initializing things
btn_ok.done = &result_ok;
// Here, btn_ok.done gets the address of result_ok..
}
main.cpp:
std::vector<messagebox> msgbox;
msgbox.push_back(msgbox());
【问题讨论】:
-
不,它没有。错误在其他地方。
-
一种猜测是包含对象被复制到某处,然后指针指向旧对象。
-
PEBCAK :) :) :)
-
请创建minimal reproducible example,以便我们看到您遇到的问题。
-
突然,无缘无故——没有“突然”。一些代码在你做那个分配的时间和你发现事情发生变化的时间之间被执行了。