I’m new to C++ and am porting over code from Java. I have a renderer class which has a method that takes an interface as an argument. This way the caller can define how to render its buffers. Now I’m running into trouble when I try to ‘new’ the ported over class. I get the error ‘allocating an object of abstract class type’. This is the part of my code that I think I need to do something about:
// Interface to the OpenGL ES renderer; consumed by GLView
struct IRenderingEngine {
virtual void initialize(int width, int height) = 0;
virtual void render() const = 0;
virtual ~IRenderingEngine() {}
};
class Renderer : public IRenderingEngine
{
public:
...
class IDrawGLBuffer
{
public:
virtual ~IDrawGLBuffer() {};
virtual void function(VBODescription vboDescription,
ShaderDescription shaderDescription);
};
class ShaderDescription
{
public:
ShaderDescription(){};
ShaderDescription(string vertexShaderText,
string fragmentShaderText,
map<string,GLint>& variableMap,
IDrawGLBuffer& callback,
int vertexDimensions);
GLuint getProgramID() const {return shaderProgramID;};
int getVertexDimensions() const {return vertexDimensions;};
friend class Renderer;
private:
string vertexShaderText;
string fragmentShaderText;
GLuint shaderProgramID;
IDrawGLBuffer callback;
int vertexDimensions;
map<string,GLint> variableMap;
};
class VBODescription
{
public:
VBODescription(){};
VBODescription(string shaderName,
map<string, Buffer>& variableMap,
GLenum usage,
int indexTotal,
GLuint textureID);
int getIndexTotal() const {return indexTotal;};
GLuint getTextureID() const {return textureID;};
friend class Renderer;
private:
GLenum usage;
map<string, Buffer> variableMap;
string shaderName;
int indexTotal;
GLuint textureID;
};
...
};
You shouldn’t, and can’t easily, include the interface directly in the class as you’ve done:
This allocates space for the interface only (which doesn’t take any room, being abstract), and you can’t know the size of any implementation (it could and probably does inherit the interface and add private members).
What you should do is pass a pointer to the implementation of the interface, and then store that:
You then call methods from the interface using the pointer syntax (
a->b(c)).Since it’s not possible for the compiler to know how big an implementation of an interface will be later, using pointers to reference the memory (which is somewhat known) becomes very important.
I used a
shared_ptrhere to help automatically release the impl, although you may need aCComPtror if your class is a bit odd,boost::intrusive_ptr. When working with interfaces, smart pointers are your friend (more than regularly).