0

The wording of this question might be a little strange. Let's get into it.

I have been developing my own DirectX11 video game engine and have recently come across an issue.

Here's the Bindable class, which is derived by other classes with the main goal of binding things like buffers and shaders to the pipeline.

class Bindable {

protected:

    static Microsoft::WRL::ComPtr<ID3D11DeviceContext> getDeviceContext(GraphicsOutput& gfx) noexcept;
    static Microsoft::WRL::ComPtr<ID3D11Device> getDevice(GraphicsOutput& gfx) noexcept;

public:

    virtual void bind(GraphicsOutput& gfx) noexcept = 0;

};

Although this class is never instantiated directly, there a numerous errors detailing that such an event has occurred.

'Bindable': cannot instantiate abstract class

One of the lines where the error occurs is in the Drawable.cpp file, which among other things, stores Bindables in a vector: std::vector<Bindable> m_binds Only derivatives of Bindable would end up in the vector, though.

#include "Drawable.h"

void Drawable::draw(GraphicsOutput& gfx) noexcept {
    for (auto& b : m_binds) {
        b.bind(gfx);
    }
    for (auto& b : getStaticBinds()) {
        b.bind(gfx);
    }
    gfx.Draw(m_pVertexBuffer->getCount());
}

void Drawable::addBind(Bindable& bind) noexcept {
    m_binds.push_back(bind);
}

void Drawable::addVertexBuffer(VertexBuffer& vertexBuffer) noexcept {
    m_pVertexBuffer = &vertexBuffer;
    m_binds.push_back(vertexBuffer);
}

My question is: Is there any way around this? When Drawable::addBind() is called, the parameter being passed will be a derivative of Bindable and when m_binds.push_back(bind); is executed, it should add bind, an instantiation of an object that is derived from Bindable, to the m_binds vector. What gives?

  • 2
    You are experiencing [object slicing](https://stackoverflow.com/questions/274626/what-is-object-slicing). You cannot have a `std::vector` where each element is actually some concrete type derived from `Bindable`. But you could have a `std::vector>` or some other indirection. – Nathan Pierson May 23 '22 at 06:46

0 Answers0