c++ - 如何在 C++ 的 MFC 窗体中访问全局变量

标签 c++ variables visual-c++ global-variables

我在 Windows 窗体 C++ 项目中实现了以下内容

template<class T> class MyQueue
{
    T *m_data;
    int m_numElements;

public:
    MyQueue() : m_data(NULL), m_numElements(0) { }

  ..... code .....

};

MyQueue<char> logData; // I need to acces it from Form1.h

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{

    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    Application::Run(gcnew Form1());

    return 0;
}

我想在 Form1.h 中访问它 下

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
{
  ??? MyQueue<char> logData; // I need to acces it 
}

有什么线索吗?

最佳答案

您可以将logdata 声明为静态的。访问原始数据成员通常不被认为是好的做法,因此您可能还想提供一个静态方法来将字符放入队列中。这是有关 C++ 静态成员的教程。

http://www.tutorialspoint.com/cplusplus/cpp_static_members.htm

关于c++ - 如何在 C++ 的 MFC 窗体中访问全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656248/

相关文章:

c - 尝试将地址传递给简单变量到 C 中的函数(指针和循环)

c++ - 在 VC++ 中通过正则表达式拆分字符串

c++ - 检测字符串中的 Unicode 字符

c++ - 为什么使用 Visual Studio 6 for C++

c++ - 将输入文件中的数据位置更改为.cpp程序

c++ - 调试和发布版本之间可能出现的差异?

c++ - C++中抽象类的内联继承

javascript - 如何遍历具有相同前缀的id

python - tkinter 中的变量替换

c++ - 与 glfw3 链接时出错