【发布时间】:2013-07-22 13:54:51
【问题描述】:
我已经定义了一个函数
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 CreateDialog\WM_Command\CreateHtmlPreview();)
//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.
}
它在我的程序中的某处被声明为静态的,如下所示:
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() 函数定义中可访问?
【问题讨论】:
-
当我读到标题时,我就像 - “是的 - 没错”......
-
问题不应该是如何欺骗语言将非静态数据暴露到静态上下文中,而是如何修复您的设计
-
为什么不将
AMEPreviewHandler对象的实例传递给CreateHtmlPreview函数?如果您需要访问非静态成员变量,或者使函数不是静态的。 -
Joachim 不可能使它成为非静态的.. 因为如果我这样做我将无法在静态 DialogProc 函数中使用 CreateHtmlPreview().. 还有其他方法吗?你明白我的问题了吗??
-
我可以建议一个小的重新设计提示吗?如果您可以制作一个封装 m_pStream 的 HtmlPreview 对象并在其上实现非静态函数 Create(),那不是很好吗?因此,您可以进行更 C++ 风格的 HtmlPreview.Create() 调用,而不是更 C 风格的 CreateHtmlPreview()。
标签: c++ winapi dialog static-methods static-members