Non-static members not accessible in static functi

2019-03-06 00:02发布

I have defined a 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 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.  

}

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;
    }  
}

}

So what can be done to make the value of non static m_pStream accessible in the static CreateHtmlPreview() function definition ?

4条回答
对你真心纯属浪费
2楼-- · 2019-03-06 00:31

Can't you just pass the m_pStream var as a function argument?

Instead of defining the function this way

HRESULT AMEPreviewHandler:: CreateHtmlPreview()
{

    ULONG  CbRead;
    const int Size= 115000;
    char Buffer[Size+1];
    HRESULT hr = m_pStream->Read(Buffer, Size, &CbRead );

}

You can do it like this (you should define the stream type!)

HRESULT AMEPreviewHandler:: CreateHtmlPreview(stream)
{

    ULONG  CbRead;
    const int Size= 115000;
    char Buffer[Size+1];
    HRESULT hr = stream->Read(Buffer, Size, &CbRead );

}

And call it like this

CreateHtmlPreview(m_pStream);
查看更多
Root(大扎)
3楼-- · 2019-03-06 00:37

In static class functions you can access to only static class members.

查看更多
太酷不给撩
4楼-- · 2019-03-06 00:46

DoctorLove i have solved this problem actually the code by idea of accesing the non static varible using this parameter- the problem was i had not initialized the instance in WM_INITDIALOG now i donze like this-

case WM_INITDIALOG: 
            {

                instance = (AMEPreviewHandler*)lParam;
                instance->m_pStream;
return0;
}

and it works fine.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-03-06 00:52

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)
{
  //...
}

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

static because i am calling this function from DialogProc function

however, the DialogProc is not static (in the code you have posted), so I don't see what the problem would be.

查看更多
登录 后发表回答