2

Hello everyone and thank you for looking. This is a follow up to the original question posted here.

I have a base class that I define thusly:

class DrawableShape
{
public:
    virtual HRESULT DrawShape(ID2D1HwndRenderTarget* m_pRenderTarget)
    {
        return S_OK;
    }
};

I have two classes that extend this class, both are similar, so I'm listing one:

class MyD2DEllipse : public DrawableShape
{
private:
    D2D1_ELLIPSE data;
public:
    MyD2DEllipse();
    HRESULT DrawShape(ID2D1HwndRenderTarget* m_pRenderTarget);
};

The DrawShape function is implemented like this:

HRESULT MyD2DEllipse::DrawShape(ID2D1HwndRenderTarget* m_pRenderTarget)
{
    HRESULT hr = E_FAIL;
    ID2D1SolidColorBrush* m_pBrush;
    hr = m_pRenderTarget->CreateSolidColorBrush(
                D2D1::ColorF(D2D1::ColorF::OrangeRed),
                &m_pBrush
                );
    m_pRenderTarget->DrawEllipse(&data, m_pBrush, 10.f);
    return hr;
}

I want to draw a random number of ellipses and rectangles to the screen, so I first find out those random numbers, create an array of DrawableShape with that size (since I can't allocate objects dynamically in C++), replace the parent objects with the child objects, and then call the draw function on the array, randomly again. Here's what my code looks like:

    HRESULT Demo::OnRender()
    {
        HRESULT hr = S_OK;
        hr = CreateDeviceResources();

        if (SUCCEEDED(hr))
        {
            m_pRenderTarget->BeginDraw();
            m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
            m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));

            // Decide on # of primitives
        randEllipse = 1 + (rand() % 5);
        randRectangle = 1 + (rand() % 5);
        totalPrimitives = randEllipse + randRectangle;

        DrawableShape *shapes;
        shapes = new MyShape[totalPrimitives];

        for (int i=0; i<randEllipse; i++)
        {
            MyEllipse ellipse1;
            shapes[i] = ellipse1;
        }
        for (int i=randEllipse; i<(randEllipse + randRectangle); i++)
        {
            MyRectangle rect1;
            shapes[i] = rect1;
        }

        for (int i=0; i<totalPrimitives; i++)
        {
            hr = shapes[i].DrawMyShape(m_pRenderTarget);
        }
        hr = m_pRenderTarget->EndDraw();
    }
}

That should've worked, but it doesn't. Also, after writing this out, I realize that I'm better off creating the array in some sort of init function, and calling the draw on the array in the OnRender function. Please help!!


EDIT: Okay I've got the shapes working with pointers, the problem is the construction of the array. So I have something like this:

    MyD2DRectangle rect1;
    MyD2DEllipse ell1;

    DrawableShape *shape1 = &rect1;
    DrawableShape *shape2 = &ell1;
    shape1->DrawShape(m_pRenderTarget);
    shape2->DrawShape(m_pRenderTarget);

That seems to work by itself. How can I create the array of DrawableShape without slicing?

4

1 回答 1

4

shapes是一个MyShape实例数组。当您说shapes[i] = ellipse1;or时,您将丢失作为此分配的一部分的子类数据,这在 C++ 中shapes[i] = rect1;称为切片。

因此,每次调用shapes[i].DrawMyShape(m_pRenderTarget);都只是S_OK按照中定义的方式返回MyShape

为了在 C++ 中正确使用多态性,您需要使用指向MyShape实例的指针或引用(通常使用 分配new)。如果您不允许这样做(作业?),那么您需要找到一种方法来做到这一点,而无需多态性。

于 2011-01-20T21:49:10.070 回答