[英]Accessing struct members by a pointer
I have a multi-instance dialog, whose HWND and DC are contained in a vector of structs. 我有一个多实例对话框,其HWND和DC包含在结构向量中。 Before I call
CreateWindowEx()
, I allocate memory for a new instance of the struct and pass its pointer to WndProc (with lpParam
). 在调用
CreateWindowEx()
之前,我为该结构的新实例分配了内存,并将其指针传递给WndProc(使用lpParam
)。
Inside the WndProc I have the following code: 在WndProc内部,我有以下代码:
HexParams Hex;
HexParams *pHex;
if (uMsg == WM_NCCREATE) {
SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR) ((CREATESTRUCT *)lParam)->lpCreateParams);
return TRUE;
} else {
LONG_PTR lpUserData = GetWindowLongPtr(hDlg, GWLP_USERDATA);
if (lpUserData) {
pHex = (HexParams *)lpUserData;
Hex = *pHex;
} else
return DefWindowProc(hDlg, uMsg, wParam, lParam);
}
Then throughout the whole WndProc I'm constantly using Hex.Member
to acces its memebrs, read from them and write to them (if was left from when it was a single instance and the struct was global). 然后在整个WndProc中,我一直在使用
Hex.Member
来访问其memebr,对其进行读取并对其进行写入(如果是在单个实例且该结构为全局结构时遗留下来的)。 So when I was adding multi-instance support, I hoped that simply dereferencing the pointer would give me the struct memebrs from that vector. 因此,当我添加多实例支持时,我希望简单地取消引用指针就可以使我从该向量获得struct memebres。
However, the following examples do different things: 但是,以下示例做了不同的事情:
Hex.DC = GetDC(hDlg);
pHex->DC = GetDC(hDlg);
Despite of doing Hex = *pHex;
尽管执行了
Hex = *pHex;
before it. 在它之前。
pHex->DC
writes to my vector element, but Hex.DC
writes to somewhere I can't figure out. pHex->DC
写入我的向量元素,但是Hex.DC
写入我无法弄清楚的地方。 Is there a way to preserve Hex.Member
usage in the code, or I'll have to convert them all to pHex->Member
? 有没有办法在代码中保留
Hex.Member
用法,或者我必须将它们全部转换为pHex->Member
?
I may be "missing the point of pointers", but I can't get how to properly use them here. 我可能是“缺少指针的要点”,但是在这里我无法获得如何正确使用它们的信息。
Hex
is a variable defined in that function. Hex
是在该函数中定义的变量。 Writing Hex.DC
will refer to 4 bytes within that structure, on the stack of this function call. 在此函数调用的堆栈上,写入
Hex.DC
将引用该结构内的4个字节。
pHex
points somewhere determined by the caller. pHex
指向呼叫者确定的某个地方。 Hex = *pHex;
will copy the contents of the arriving structure to your local structure. 会将到达的结构的内容复制到您的本地结构。 They are different blocks of memory.
它们是不同的内存块。
Draw a picture of the stack, block out the activation frame of a call, fill in Hex there. 绘制堆栈的图片,遮挡呼叫的激活帧,然后在其中填充十六进制。 Draw other blocks where memory is allocated (global, dynamic, earlier calls) and draw arrows to them to indicate pointers.
绘制其他分配了内存的块(全局,动态,早期调用),并向其绘制箭头以指示指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.