【问题标题】:Non-static members not accessible in static function静态函数中不可访问的非静态成员
【发布时间】: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


【解决方案1】:

在静态类函数中,您只能访问静态类成员。

【讨论】:

  • 是的,我知道,但是在这种情况下,我需要访问非静态的 m_pStream ..如果我将其声明为静态,则使用此 m_pStream 的其他非静态成员将不再是可以访问它
【解决方案2】:

如果您将CreateHtmlPreview() 设为免费函数会怎样?
如果你让它只是创建一个 html 预览(而不是从流中读取)呢?

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

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

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

您可能需要让函数返回预览以供使用。
你确实说你需要做到这一点

静态因为我是从 DialogProc 函数调用这个函数

但是,DialogProc 不是静态的(在您发布的代码中),所以我看不出会出现什么问题。

【讨论】:

  • 我不知道为什么我需要将 DialogProc 函数设为静态,否则会在此处出错 m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this);那 m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this);无法转换为 DLGPROC,当我这样做时,一切正常
  • 显示实际代码和你得到的实际错误(并尝试没有静态)
  • 因为 CreateDialogParam 需要一个指向简单函数的指针,而不是指向某个类的成员函数的指针。
  • 那么问题中缺少重要的细节
  • m_pStream 成员应该是唯一的还是应该在 AMEPreviewHandler 的所有实例中通用?
【解决方案3】:

您不能只将 m_pStream 变量作为函数参数传递吗?

不是这样定义函数

HRESULT AMEPreviewHandler:: CreateHtmlPreview()
{

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

}

你可以这样做(你应该定义流类型!)

HRESULT AMEPreviewHandler:: CreateHtmlPreview(stream)
{

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

}

然后这样称呼它

CreateHtmlPreview(m_pStream);

【讨论】:

  • 不,我不能这样做,因为 CreateHtmlPreview() 是从同样是静态的 DialogProc 函数中获得的......并且由于 DialogProc 我需要将其设为静态,否则我不感兴趣给自己制造问题..您还有其他解决方案吗??
【解决方案4】:

DoctorLove 我已经通过使用这个参数访问非静态变量的想法解决了这个问题-问题是我没有在 WM_INITDIALOG 中初始化实例现在我像这样打盹-

case WM_INITDIALOG: 
            {

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

而且效果很好。

【讨论】:

    猜你喜欢
    • 2011-09-30
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多