简体   繁体   English

在类C ++中初始化静态数据成员(类)

[英]Initializing a static data member (class) within a class C++

I am trying to declare a static class within a parent class and initialize it, but I seem to be getting all sorts of errors. 我试图在父类中声明一个静态类并初始化它,但我似乎得到了各种各样的错误。

/* MainWindow.h */
    class MainWindow
    {
        private:
        static DWORD WINAPI threadproc(void* param);
        static MainWindow *hWin;
    };
/* MainWindow.cpp */
#include "MainWindow.h"
      void MainWindow::on_pushButton_clicked()
        {
            HANDLE hThread = CreateThread(NULL, NULL, threadproc, (void*) this, NULL, NULL);
            WaitForSingleObject(hThread, INFINITE);
            CloseHandle(hThread);
        }

        DWORD WINAPI MainWindow::threadproc(void* param)
        {
            hWin = (MainWindow*) param;
            //Be able to access stuff like hWin->run();
            return 0;
        }

I have tried using MainWindow::hWin = (MainWindow*) param; 我尝试过使用MainWindow::hWin = (MainWindow*) param; and MainWindow::hWin = new MainWindow((MainWindow*) param)); MainWindow::hWin = new MainWindow((MainWindow*) param)); and many others, but none seem to work. 和许多其他人一样,但似乎没有工作。 What is the proper way to do this? 这样做的正确方法是什么? Are there any resources anybody would recommend on this subject, I have been tangling with class problems for a few days now and am very frustrated. 有没有人会就这个问题推荐任何资源,我现在已经纠结了几天的class问题而且非常沮丧。

Static members always consist of a declaration and a definition , you lack the definition in your cpp file. 静态成员总是由声明定义组成 ,缺少cpp文件中的定义。 Put the following line outside of any functions: 将以下行放在任何函数之外:

MainWindow* MainWindow::hWin;

You can read more here or here . 您可以在这里这里阅读更多内容。

Using a static variable like in your example will not allow you to have more than one instance, so it's best to avoid it if possible. 使用类似示例的静态变量不允许您拥有多个实例,因此最好尽可能避免使用它。 And in your example there is no need to use one, you can easily use a local variable instead. 在您的示例中,不需要使用一个,您可以轻松地使用局部变量。

Just remove the static MainWindow *hWin; 只需删除static MainWindow *hWin; from your class definition, and modify MainWindow::threadproc() to use a local variable: 从您的类定义,并修改MainWindow :: threadproc()以使用局部变量:

    DWORD WINAPI MainWindow::threadproc(void* param)
    {
        MainWindow* const hWin = static_cast<MainWindow*>(param);
        //hWin->whatever();
        return 0;
    }

However, if you really want to/have to use a static variable (for reasons that are not apparent in your example), then I'd suggest to set it in MainWindow's ctor - and just there. 但是,如果你真的想要/必须使用静态变量(由于你的例子中不明显的原因),那么我建议在MainWindow的ctor中设置它 - 就在那里。 No need to explicitly pass it to the thread. 无需将其显式传递给线程。

    MainWindow::MainWindow()
    {
        assert(hWin == 0);
        hWin = this;
    }

    MainWindow::~MainWindow()
    {
        assert(hWin == this);
        hWin = 0;
    }

    void MainWindow::on_pushButton_clicked()
    {
        HANDLE hThread = CreateThread(0, 0, threadproc, 0, 0, 0);
        WaitForSingleObject(hThread, INFINITE);
        CloseHandle(hThread);
    }

    DWORD WINAPI MainWindow::threadproc(void*)
    {
        // just use hWin, it already points to the one and only MainWindow instance
        return 0;
    }

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

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