如何在 C++/CLI 代码中初始化非托管静态指针?

发布于 2024-08-02 05:33:57 字数 1103 浏览 3 评论 0原文

我无法在托管类中初始化静态本机指针。

详细信息如下:我创建了一个 C++/CLI 控制台项目并在内部声明了一个静态非托管指针。但是,我无法用任何方法初始化静态指针(但是如果我将指针放入匿名命名空间中,就没有问题,在代码中进行了演示)。您是否遇到过类似的情况并有解决方案?

代码在这里:

class MyTask
{
public:
    int m_index;
    MyTask()
    {
        m_index = 0;
    }
};

namespace
{
    static MyTask* s_pTask;
}

public ref class MyApplication
{
public:
    static MyTask* sm_pTask;

    static void InitizlizeStaticMembers(MyTask* pTask)
    {
        sm_pTask = pTask;
    }

    void AddTask()
    {
        sm_pTask = new MyTask();
    }
};

void main()
{
    MyApplication^ app = gcnew MyApplication();

    // 1st, doesn't work (sm_pTask is still null after the execution)
    MyApplication::sm_pTask = new MyTask();
    // 2nd, doesn't work (pTask can be initialized correctly, sm_pTask is still null after the execution)
    MyTask* pTask = new MyTask();
    MyApplication::InitizlizeStaticMembers(pTask);
    // 3rd, doesn't work (sm_pTask is still null after the execution)
    app->AddTask();
    // 4th, work(s_pTask can be initialized correctly)
    s_pTask = new MyTask();
}

I'm not able to initialize a static native pointer inside a managed class.

Here's the detail: I created a C++/CLI console project and declared a static unmanaged pointer inside. However I can't initialize the static pointer with any means (but if I put the pointer into an anonymous namespace, there's no problem, demoed in the code). Did u meet similar situations and got solutions for it?

Code here:

class MyTask
{
public:
    int m_index;
    MyTask()
    {
        m_index = 0;
    }
};

namespace
{
    static MyTask* s_pTask;
}

public ref class MyApplication
{
public:
    static MyTask* sm_pTask;

    static void InitizlizeStaticMembers(MyTask* pTask)
    {
        sm_pTask = pTask;
    }

    void AddTask()
    {
        sm_pTask = new MyTask();
    }
};

void main()
{
    MyApplication^ app = gcnew MyApplication();

    // 1st, doesn't work (sm_pTask is still null after the execution)
    MyApplication::sm_pTask = new MyTask();
    // 2nd, doesn't work (pTask can be initialized correctly, sm_pTask is still null after the execution)
    MyTask* pTask = new MyTask();
    MyApplication::InitizlizeStaticMembers(pTask);
    // 3rd, doesn't work (sm_pTask is still null after the execution)
    app->AddTask();
    // 4th, work(s_pTask can be initialized correctly)
    s_pTask = new MyTask();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏至、离别 2024-08-09 05:33:57

我发现(使用VS2005)调试器证实了你所说的——指针的值在更新时不会改变。但如果你让它输出一些东西,代码实际上是有效的。

我相信这是调试器中的一个错误。

I found that (with VS2005) the debugger confirms what you said - the value of the pointer doesn't change as it gets updated. But the code is actually working if you make it output something.

I believe this is a bug in the debugger.

岁月无声 2024-08-09 05:33:57

我没有看到代码有任何问题,它对我来说工作得很好。不需要匿名名称空间。

#include "stdafx.h"
#include <iostream>

class MyTask
{
public:
    int m_index;
    MyTask()
    {
        m_index = 0;
    }
};

static MyTask* s_pTask;

public ref class MyApplication
{
public:
    static MyTask* sm_pTask = NULL;

    static void InitizlizeStaticMembers(MyTask* pTask)
    {
        sm_pTask = pTask;
    }

    void AddTask()
    {
        sm_pTask = new MyTask();
    }
};

void main()
{
    MyApplication^ app = gcnew MyApplication();

    // 1st, doesn't work (sm_pTask is still null after the execution)
    std::cout << "MyApplication::sm_pTask before new " << MyApplication::sm_pTask << '\n'; // 0
    MyApplication::sm_pTask = new MyTask();
    std::cout << "MyApplication::sm_pTask after new " << MyApplication::sm_pTask << '\n'; // 003B8170

    MyTask* pTask = new MyTask();
    std::cout << "pTask= " << pTask << '\n'; // 003B81A0
    MyApplication::InitizlizeStaticMembers(pTask);
    std::cout << "MyApplication::sm_pTask = " << MyApplication::sm_pTask << '\n'; // 003B81A0

    app->AddTask();    
    s_pTask = new MyTask();
}

I don't see any issue with the code and it works fine for me. The anonymous namespace is not needed.

#include "stdafx.h"
#include <iostream>

class MyTask
{
public:
    int m_index;
    MyTask()
    {
        m_index = 0;
    }
};

static MyTask* s_pTask;

public ref class MyApplication
{
public:
    static MyTask* sm_pTask = NULL;

    static void InitizlizeStaticMembers(MyTask* pTask)
    {
        sm_pTask = pTask;
    }

    void AddTask()
    {
        sm_pTask = new MyTask();
    }
};

void main()
{
    MyApplication^ app = gcnew MyApplication();

    // 1st, doesn't work (sm_pTask is still null after the execution)
    std::cout << "MyApplication::sm_pTask before new " << MyApplication::sm_pTask << '\n'; // 0
    MyApplication::sm_pTask = new MyTask();
    std::cout << "MyApplication::sm_pTask after new " << MyApplication::sm_pTask << '\n'; // 003B8170

    MyTask* pTask = new MyTask();
    std::cout << "pTask= " << pTask << '\n'; // 003B81A0
    MyApplication::InitizlizeStaticMembers(pTask);
    std::cout << "MyApplication::sm_pTask = " << MyApplication::sm_pTask << '\n'; // 003B81A0

    app->AddTask();    
    s_pTask = new MyTask();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文