简体   繁体   English

函数调用继承C ++

[英]Function Calling Inheritance C++

Okay firstly Inheritance isn't my strong point that is why I am here. 首先,继承不是我的强项,这就是为什么我在这里。 However i will explain the best i can. 但是我会尽力而为。

I am calling a function from Class 1 in Class 2, Class 2 is derived from Class 1 and Class 2 is the base class for Class 3. 我正在从Class 2中的Class 1调用函数,Class 2是从Class 1派生的,Class 2是Class 3的基类。

Upon execution of the program it is class 3 which is called in WinMain(). 程序执行后,将在WinMain()中调用类3。

Ill show it code always makes sense that way: 糟糕的是,代码总是以这种方式有意义:

void SRNTY_API Direct3D11::D3D11ResizeBuffers(HWND hwnd)
{
    RECT rect;
    GetWindowRect(hwnd, &rect);
    mRenderTargetWidth = rect.right - rect.left;
    mRenderTargetHeight = rect.bottom - rect.top;

    if (mDXGISwapChain != NULL)
    {
        assert(mD3D11DeviceContext);
        assert(mD3D11Device);
        assert(mDXGISwapChain);

        if (mD3D11DeviceContext)
        {
            mD3D11DeviceContext->ClearState();
        }
        if (mD3D11RenderTargetView)
        {
            mD3D11RenderTargetView->Release();
        }
        if (mD3D11DepthStencilView)
        {
            mD3D11DepthStencilView->Release();
        }

        if (FAILED(result = mDXGISwapChain->ResizeBuffers(1, mRenderTargetWidth, mRenderTargetHeight,
            DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH)))
        {
            SRNTY::ErrorHandler::Instance()->AddError(L"Direct3D11::D3D11ResizeBuffers() - ID3D11Device::CreateRenderTargetView() failed to create render target view!",
                SRNTY::ErrorHandler::ErrorHeaderSelect::EHS_ERROR);
        }

        if (FAILED(result = mDXGISwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mD3D11RenderTargetView)))
        {
            SRNTY::ErrorHandler::Instance()->AddError(L"Direct3D11::D3D11ResizeBuffers() - ID3D11Device::CreateDepthStencilView() failed to create depth stencil view!!",
                SRNTY::ErrorHandler::ErrorHeaderSelect::EHS_ERROR);
        }

        if (FAILED(result = mDXGISwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mD3D11DepthStencilView)))
        {
            SRNTY::ErrorHandler::Instance()->AddError(L"Direct3D11::D3D11ResizeBuffers() - ID3D11Device::CreateDepthStencilView() failed to create depth stencil view!!",
                SRNTY::ErrorHandler::ErrorHeaderSelect::EHS_ERROR);
        }

        D3D11_VIEWPORT viewport;
        ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

        viewport.TopLeftX = 0;
        viewport.TopLeftY = 0;
        viewport.Width = mRenderTargetWidth;
        viewport.Height = mRenderTargetHeight;
        viewport.MinDepth = 0.0f;
        viewport.MaxDepth = 1.0f;

        if ((mD3D11DeviceContext != NULL) && (mD3D11RenderTargetView != NULL) && (mD3D11DepthStencilView != NULL))
        {
            mD3D11DeviceContext->OMSetRenderTargets(1, &mD3D11RenderTargetView, mD3D11DepthStencilView);
            mD3D11DeviceContext->RSSetViewports(1, &viewport);
        }
    }
}

The above function is designed to re size the IDXGISwapChain* buffers. 上面的函数旨在调整IDXGISwapChain *缓冲区的大小。 So this must be called on WM_EXITSIZEMOVE. 因此,必须在WM_EXITSIZEMOVE上调用它。 This function would be be within Class 1 from the above blabba. 该函数在上述blabba的Class 1中。

So the next class which uses the above functions class as a base now calls this on WM_EXITSIZEMOVE, here is the code: 因此,使用上述函数类作为基础的下一个类现在在WM_EXITSIZEMOVE上调用此代码,代码如下:

LRESULT Window::MsgProc(__in HWND hWnd, __in UINT message,
    __in_opt WPARAM wParam, __in_opt LPARAM lParam)                 
{
    switch (message)
    {
    case WM_KEYDOWN:                                                                    
    {
        if (wParam == VK_ESCAPE)                                                        
        {
           PostQuitMessage(0);
        }
    }

    case WM_MENUCHAR:                                                                   
    {
        return MAKELRESULT(0, MNC_CLOSE);                                       
    } break;

    case WM_DESTROY:                                                                        
    {
        PostQuitMessage(0);                                                             
    } break;

    case WM_ENTERSIZEMOVE:                                                          
    {

    } break;

    case WM_EXITSIZEMOVE:                                                               
    {
        D3D11ResizeBuffers(hWnd);    // heres is the above function being called as mentioned on WM_EXITSIZEMOVE
    } break;

    case WM_GETMINMAXINFO:                                                          
    {

    } break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

Both of these classes then get added to a Engine class which is then added to a Application class. 然后将这两个类都添加到Engine类,然后将它们添加到Application类。 The application is the front facing class to WinMain(). 该应用程序是WinMain()的前端类。

Now what is happening isn't in fact the buffers being re sized, whats happening is Direct3D11::D3D11ResizeBuffers() is getting the window detentions from calling GetWindowRect(hwnd, &rect); 现在发生的事情实际上不是正在调整缓冲区大小,而是Direct3D11 :: D3D11ResizeBuffers()正在通过调用GetWindowRect(hwnd,&rect);获取窗口滞留。 and storing the results in RECT rect;. 并将结果存储在RECT rect;中。

However when we get to this line here mRenderTargetWidth = rect.right - rect.left;. 但是,当我们到达此行时,这里的mRenderTargetWidth = rect.right-rect.left;。 It basically says mRenderTargetWidth "Exception thrown: write access violation. this was 0x8.". 它基本上说mRenderTargetWidth“抛出了异常:写访问冲突。这是0x8。”。 When i Debug the program i notice all the members of the Direct3D11 class say the following: 当我调试程序时,我注意到Direct3D11类的所有成员都说以下内容:

result Unable to read memory 结果无法读取内存
mhWnd Unable to read memory mhWnd无法读取内存
mRenderTargetWidth Unable to read memory mRenderTargetWidth无法读取内存
mRenderTargetHeight Unable to read memory mRenderTargetHeight无法读取内存
mDXGISwapChain Unable to read memory mDXGISwapChain无法读取内存
mDXGIDevice Unable to read memory mDXGIDevice无法读取内存
mDXGIAdapter Unable to read memory mDXGIAdapter无法读取内存
mDXGIFactory Unable to read memory mDXGIFactory无法读取内存
mD3D11Device Unable to read memory mD3D11Device无法读取内存
mD3D11DeviceContext Unable to read memory mD3D11DeviceContext无法读取内存

mD3D11RenderTargetView Unable to read memory mD3D11RenderTargetView无法读取内存
mD3D11DepthStencilView Unable to read memory mD3D11DepthStencilView无法读取内存

So in effect the class has diapered so like this->mD3D11Device is NULL. 因此,实际上该类已经变了,就像这样-> mD3D11Device为NULL。

It is very safe to say I am mega confused here. 可以肯定地说我在这里很困惑。 I really need help either explaining what i am doing wrong or suggestions as to how i can solve this problem would be greatly appreciated. 我真的需要帮助,要么解释我在做错什么,要么就如何解决此问题提出建议,将不胜感激。 If any needs any further information just ask and i will give. 如果有任何需要进一步的信息,只是问,我会给。

Thanks for reading and i hope people can help :) 感谢您的阅读,希望大家能给予帮助:)

Ok i was just being very dumb and was able to solve the problem simply by creating a global object back in the header file of the Direct3D11 class: 好吧,我只是非常笨,可以通过在Direct3D11类的头文件中创建一个全局对象来简单地解决问题:

extern SRNTY_API Direct3D11& pDirect3D11;

This enables me to then create an object in any cpp file like this: 这样,我便可以在任何cpp文件中创建一个对象,如下所示:

Direct3D11& pDirect3D11 = Direct3D11::Direct3D11();

So im using the same instance every time, well i think thats it i dont really like thinking about instances and inheritance boggles my mind a bit. 所以我每次都使用同一个实例,嗯,我想就是这样,我真的不喜欢思考实例,而继承却让我有点困惑。

But if anyone has the same problem this is the answer. 但是,如果有人遇到相同的问题,这就是答案。 Good luck to everyone else out there \\o/ 祝其他人好运\\ o /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM