Non-static members not accessible in static function(静态函数中不可访问的非静态成员)
问题描述
我已经定义了一个函数
HRESULT AMEPreviewHandler:: CreateHtmlPreview()
{
ULONG CbRead;
const int Size= 115000;
char Buffer[Size+1];
HRESULT hr = m_pStream->Read(Buffer, Size, &CbRead );
//this m_pStream is not accessible here even it is declared globally. the program is asking me to
// declare it static because this CreateHtmlPreview() function called
//inside the Static function (i mean here :-static CreateDialogWM_CommandCreateHtmlPreview();)
//but if i declare it static the two problems arised are
//(1.) It is not able to access the value of the m_pStream which is defined globally.
//(2.)If i declare it static globally then there are so many other function which are using this
// value of m_pStream are not able to access it because they are non static.
}
它在我的程序中的某处被声明为静态的,如下所示:
It is declared static somewhere in my program like this:
static HRESULT CreateHtmlPreview(); //i have declared it static because i am calling this function from DialogProc function.If i dont create it static here it dont work
//The function CreateHtmlPreview() is called inside the DialogProc function like this-
BOOL CALLBACK AMEPreviewHandler::DialogProc(HWND m_hwndPreview, UINT Umsg, WPARAM wParam, LPARAM lParam)
{......
case WM_COMMAND:
{
int ctl = LOWORD(wParam);
int event = HIWORD(wParam);
if (ctl == IDC_PREVIOUS && event == BN_CLICKED )
{
CreateHtmlPreview(); //here i am calling the function
return 0;
}
}
}
那么如何才能使非静态m_pStream
的值在静态CreateHtmlPreview()
函数定义中可访问?
So what can be done to make the value of non static m_pStream
accessible in the static CreateHtmlPreview()
function definition ?
推荐答案
如果你让 CreateHtmlPreview()
成为一个免费的函数呢?
如果你让它只是创建一个 html 预览(而不是从流中读取)呢?
What if you make CreateHtmlPreview()
a free function?
What if you make it just create an html preview (instead of also reading from a stream)?
void CreateHtmlPreview(const char * buffer, int size)
{
//...
}
然后从proc中读取数据,并在DialogProc
Then read the data from the proc, and call it in DialogProc
//...
m_pStream->Read(Buffer, Size, &CbRead );
CreateHtmlPreview(Buffer, Size);
您可能需要让函数返回预览以供使用.
你确实说你需要做到这一点
You will probably need to make the function return the preview to be any use though.
You do say you need to make it
静态因为我是从 DialogProc 函数调用这个函数
static because i am calling this function from DialogProc function
但是,DialogProc 不是静态的(在您发布的代码中),所以我看不出会出现什么问题.
however, the DialogProc is not static (in the code you have posted), so I don't see what the problem would be.
这篇关于静态函数中不可访问的非静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:静态函数中不可访问的非静态成员


基础教程推荐
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01