简体   繁体   English

访问类中的指针向量

[英]Accessing vector of pointers in a class

I'm trying to figure out a way to keep track of all the instances of a class I have made, so I can access them at any point using a title string (or int ID) 我正试图找出一种方法来跟踪我所创建的类的所有实例,因此我可以使用标题字符串(或int ID)随时访问它们

I decided to use a static vector of pointers to each instance, and then on creating each instance i'd add a pointer to it to the vector. 我决定使用指向每个实例的指针的静态向量,然后在创建每个实例时,我将向它添加指向它的指针。

This works up to a point but at one point the values inside each element of the vector seem to reset/get randomly assigned values and i can't figure out what's happening. 这可以达到一个点但是在某一点上,向量的每个元素内的值似乎重置/获得随机分配的值,我无法弄清楚发生了什么。

i'm adding the object to the vector here: 我在这里将对象添加到向量:

SWindow::SWindow(LPCWSTR WindowClass, LPCWSTR Title, UINT Style, int x, int y, int height, int width, HWND hParWnd, HINSTANCE hInstance)
    :
    x(x),
    y(y)
{
    hWnd = CreateWindowEx(NULL, WindowClass, Title, Style, x, y, height, width, hParWnd, NULL, hInstance, NULL);
    SWindows.push_back(this);

The function at which the values change is: which is a member of the SWindow class 值更改的函数是:它是SWindow类的成员

SWindow.h: SWindow.h:

static SWindow* GetSWindow(wstring ws);

SWindow.cpp: SWindow.cpp:

 SWindow* SWindow::GetSWindow(wstring ws)
{
    for (int i = 0; i < SWindow::SWindows.size(); i++)
    {
        if (SWindows[i]->title == ws)
        {
            return SWindows[i];
        }
        else
        {
        }
    }

    return 0;
}

i'm accessing the function from a different class using: 我正在使用以下方法从另一个类访问该函数:

SWindow* pPlayViewer = SWindow::GetSWindow(L"Viewer");

Also if this is a bad way to be doing what i am trying to do, let me know of a better way. 此外,如果这是一个不好的方式来做我想做的事情,让我知道一个更好的方法。 Thanks! 谢谢!

Are you sure that you didn't add stack allocated objects into your static vector? 您确定没有将堆栈分配的对象添加到静态向量中吗? Did you remove pointers when objects are deleted ? 删除对象时是否删除了指针?

If you want to be more efficient, I can suggest you to use a map, where the key can be your title string/id int and the value the pointer, so that the search would be much faster than parsing the whole array. 如果你想提高效率,我建议你使用一个地图,其中键可以是你的标题字符串/ id int和指针的值,这样搜索将比解析整个数组快得多。

There are four main possible causes for the dangling pointers: 悬空指针有四个主要可能的原因:

  • you do not remove the instances from the vector upon destruction of an instance 在销毁实例时,不要从向量中删除实例

  • you create instances accross DLL boundaries (and pass the vector arround) 你创建跨越DLL边界的实例(并传递矢量arround)

  • you have a buffer overflow (or similar) in another part of the code and it is overwriting your vector 你在代码的另一部分有一个缓冲区溢出(或类似),它会覆盖你的向量

  • you are accessing the vector concurrently from multiple threads (and the access to it doesn't look synchronized in your code) 您是从多个线程同时访问向量(并且对它的访问在您的代码中看起来不同步)

(this is all speculation on my part). (这是我的所有猜测)。

To use such a vector correctly, you will have to do the following: 要正确使用此类向量,您必须执行以下操作:

  • implement all constructors and destructor for your class (this implies you will also implement the assignment operators, according to the rule of five). 为你的类实现所有构造函数和析构函数(这意味着你也将根据五条规则实现赋值运算符)。

  • ensure all constructors add this to the vector 确保所有构造函数都this添加到向量中

  • ensure destructor removes this from the vector 确保析构函数从向量中删除this

Also, suggested refactorings: 另外,建议的重构:

  • pass the vector into the object, instead of declaring it as static; 将向量传递给对象,而不是将其声明为静态; this will allow you to decide in client code if you have a single vector, multiple ones, or a window manager object of some sort, that holds a vector internally 这将允许您在客户端代码中决定是否有单个向量,多个向量或某种类型的窗口管理器对象,它在内部保存向量

  • group the window creation parameters into a structure, and pass that arround as a parameter 将窗口创建参数分组到一个结构中,并将该arround作为参数传递

  • your SWindow class wants to be both a window manager and a window; 你的SWindow类想成为一个窗口管理器和一个窗口; Extract the window management into a separate object 将窗口管理提取到单独的对象中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM