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

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

问题描述

我定义了一个函数

  HRESULT AMEPreviewHandler :: CreateHtmlPreview()
{
ULONG CbRead ;
const int Size = 115000;
char Buffer [Size + 1];
HRESULT hr = m_pStream-> Read(Buffer,Size,& CbRead);
//这里m_pStream是不可访问的,即使它是全局声明。程序要求我
//声明它静态,因为这个CreateHtmlPreview()函数调用
//在静态函数(我的意思是这里:-static CreateDialog \WM_Command\CreateHtmlPreview();)
//但是如果我声明它静态的两个问题是
//(1。)它不能访问m_pStream的值是全局定义的。
//(2.)如果我声明它全局静态,那么有很多其他函数使用这个
// m_pStream的值是不能访问它,因为它们是非静态的。

}

在我的程序中声明为static static: / p>

  static HRESULT CreateHtmlPreview //我已经声明它静态,因为我从DialogProc函数调用此函数。如果我不创建它静态这里它不工作

//函数CreateHtmlPreview()在DialogProc函数内调用像这样 -

BOOL CALLBACK AMEPreviewHandler :: DialogProc(HWND m_hwndPreview,UINT Umsg,WPARAM wParam,LPARAM lParam)
{......
case WM_COMMAND:
{
intctl = LOWORD(wParam);
int Event = HIWORD(wParam);

if(ctl == IDC_PREVIOUS&& event == BN_CLICKED)
{
CreateHtmlPreview(); //这里我调用函数
return 0;
}
}

}



那么可以做什么使静态 CreateHtmlPreview() m_pStream >

如果你创建 CreateHtmlPreview() a,那么如何使用函数定义?

解决方案自由功能? $ b $bÚ
如果你只是创建一个html预览(而不是从流中读取),该怎么办?

  void CreateHtmlPreview(const char * buffer,int size)
{
// ...
}

然后从proc中读取数据,并在 DialogProc

  // ... 
m_pStream-> Read(Buffer,Size,& CbRead);
CreateHtmlPreview(Buffer,Size);

你可能需要让函数返回预览才能使用。 $ b $bÚ
你说你需要它


static因为我正在调用这个函数DialogProc函数


但是,DialogProc不是静态的(在你发布的代码中),所以我没有看到什么问题将是。


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 ?

解决方案

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.

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

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