静态函数中不可访问的非静态成员 [英] Non-static members not accessible in static function

查看:37
本文介绍了静态函数中不可访问的非静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个函数

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.

这篇关于静态函数中不可访问的非静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆