Parameters referenced from the same variable point to different locations

元气小坏坏 提交于 2019-12-12 03:48:44

问题


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;
}

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?

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.


回答1:


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.

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)



来源:https://stackoverflow.com/questions/14112528/parameters-referenced-from-the-same-variable-point-to-different-locations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!