It might be a weird question, but it could be quite handy in my use case.
I have the generic class:
template <typename CGtObj>
class CGtObjectsMap
{
virtual CGtObj* newGtObject(GtId idXML, const CSAXElement *) const { return new CGtObj(idXML); } //!< can be overwritten by MyAbstractClass ;)
virtual void loadFromXml(CSAXElement const* element);
};
with a "quite complex" loadFromXml(CSAXElement const* element)
that has at one point:
template <typename CGtObj>
void CGtObjectsMap<CGtObj>::loadFromXml(const CSAXElement *element)
{
...
objPtr = newGtObject(idXML, element);//new CGtObj(idXML);
...
}
A certain class I need to use in that CGtObjectsMap
is abstract. That is why I've created the virtual method newGtObject
, hoping I could avoid duplicating the whole code of loadFromXml()
in a specialization of that class.
But it doesn't compile, and I'm having an error on the template standard definition of newGtObject()
:
error: invalid new-expression of abstract class type ‘MyAbstractClass’
coming from:
virtual CGtObj* newGtObject(GtId idXML, const CSAXElement *) const { return new CGtObj(idXML); } //!< can be overwritten by MyAbstractClass ;)
although I've specialized it:
template<>
MyAbstractClass *CGtObjectsMap<MyAbstractClass>::newGtObject(GtId idXML, const CSAXElement *element) const
{
MyAbstractClassType type = static_cast<MyAbstractClassType>(element->childValueByName(XML_TYPE).toInt());
return MyAbstractClass::createAction(type, idXML);
}
Is there a way to avoid that compilation issue?
(The specialization is in another file)