1

Is it normal to have to update assertions after upgrading the compiler from VC++ 6 to MSVC 2005? I have the following function which works without triggering the assertion in Visual Studio 6 but anything newer it fails.

void CMainFrame::OnUpdateGraphValue (CCmdUI* pCmdUI) {

    BOOL bMax;

    CMDIChildWnd *child = MDIGetActive (&bMax);
    if (child)
    {
        if (child->IsKindOf (RUNTIME_CLASS (CGaugeChildFrame)))
        {
            CGaugeView *pView = (CGaugeView *) child->GetActiveView ();
            if (pView->wndActive)
            {
                ASSERT (pView->IsKindOf (RUNTIME_CLASS (CGaugeView)));
                pCmdUI->Enable (TRUE);
                return;
            }
        }
        if (child->IsKindOf (RUNTIME_CLASS (CGarterChildFrame)))
        {
            CGarterView *pView = (CGarterView *) child->GetActiveView ();
            if (pView->wndGraphics)
            {
                ASSERT (pView->IsKindOf (RUNTIME_CLASS (CGarterView)));
                pCmdUI->Enable (TRUE);
                return;
            }
        }
    }

pCmdUI->Enable (FALSE); }

The failure occurs on line ASSERT (pView->IsKindOf (RUNTIME_CLASS (CGaugeView))); When I click print preview the type is not CGaugeView but CPreviewView.

Can someone please shed some light on this for me? Thanks

4

1 回答 1

2

在检查类型兼容之前强制转换为类型是无效的。

所以你需要这样做:

   if(child->GetActiveView ()->IsKindOf(RUNTIME_CLASS(CGaugeView)))
   {
        CGaugeView *pView = (CGaugeView *) child->GetActiveView ();

至于为什么这种行为发生了变化,我不知道。也许在你忽略断言之前?也许您没有尝试构建调试?

还是版本 7 中的打印预览架构发生了变化?可能以前的版本没有pView->wndGraphics打印预览模式,所以代码路径从来没有被触发过。

但是,由于您没有将代码路径用于任何内容,因此可能只是将其转储。

于 2013-01-29T17:02:05.877 回答