从相同变量指向不同位置的参数 [英] Parameters referenced from the same variable point to different locations
问题描述
我试图将HWND指针与其他数据一起存储在int向量中,我正在使用以下代码获取数据并将其存储在创建时:
I'm trying to store HWND pointers in an int vector along with other data, I'm using the following code to get the data and store it on creation:
void createscalingwindow(HWND &cswpara0,DWORD cswpara1,const CHAR* cswpara2,
const CHAR* cswpara3,DWORD cswpara4,int cswpara5,
int cswpara6,int cswpara7,int cswpara8,HWND cswpara9,
HMENU cswpara10,HINSTANCE cswpara11,LPVOID cswpara12)
{
cswpara0 = CreateWindowEx (cswpara1, cswpara2, cswpara3, cswpara4, cswpara5,
cswpara6,cswpara7,cswpara8,cswpara9,cswpara10,
cswpara11,cswpara12);
sizevalues.push_back((int)&cswpara0);
snprintf (buffer, 20,"%d", sizevalues[zero]);
MessageBox (NULL, buffer, "pointer", NULL);
sizevalues.push_back(cswpara5);
sizevalues.push_back(cswpara6);
sizevalues.push_back(cswpara7);
sizevalues.push_back(cswpara8);
return;
}
下面的代码是一个原型,当前仅显示消息框中的值,但是我后来计划让它调整子窗口的大小以与父窗口一起缩放
This following code is a prototype that currently only shows the values in a messagebox, but I later plan to have it resize child windows to scale with the parent
void scalewindowsize (HWND &ownerwin, HWND &childwin)
{
/*check owner window*/
char buffer[100];
int checknumber = 0;
while (checknumber < sizevalues.size())
{
if (sizevalues[checknumber] == (int)&ownerwin)
{
snprintf (buffer, 100,"%d", sizevalues[checknumber]);
MessageBox (NULL, buffer, "foundit", NULL);
break;
}
snprintf (buffer, 20,"%d", (int)&ownerwin);
checknumber = (checknumber + 5);
MessageBox (NULL, buffer, "fail", NULL);
}
return;
}
问题在于,createscalingwindow
中的第一个Messagebox
生成的值为4235304,而第二个生成的数字完全不同(该数字有所不同).为什么会这样?
The problem is that the first Messagebox
in createscalingwindow
produces a value of 4235304 while the second one produces an entirely different number (the number varies). Why is this?
更新:找出原因的一部分,为了重现此问题,必须在与该窗口过程中的参数HWND相同的窗口过程中使用作为scalewindowsize
参数的HWND.
UPDATE: Found out part of the cause, in order to reproduce this the HWND used as a parameter to scalewindowsize
must be used in a window procedure with the same parameter HWND in that window procedure.
推荐答案
不要在int
向量中存储非int
值.那是麻烦.
Don't store non-int
values in an int
vector. That's asking for trouble.
相反,创建一个具有所有值字段(具有正确类型)的类,并创建一个包含该类对象的向量.
Instead create a class that has fields (with the proper types) for all your values, and create a vector that contains objects of that class.
然而,您的号码更改很可能是由于您获取了局部变量的地址,并在声明变量的函数返回后使用了它.您应该只推送HWND
的值,而不是存储它的地址.句柄是纯数字,因此不需要通过引用传递它们,除非您打算在函数中进行更改(我也不知道为什么也需要在createscalingwindow
中进行此操作-您可以只返回值)
Your numbers changing is however likely caused by you taking the address of a local variable and using it after the function declaring the variable returns. You should just push the value of the HWND
, not the address where it's stored. Handles are plain numbers, so there's no need to pass them by reference, unless you plan to change them in the function (I don't see why you'd need to do that in createscalingwindow
either - you could just return the value)
这篇关于从相同变量指向不同位置的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!