Work around for being unable to make function templates virtual?-Collection of common programming errors
Well, normally, you’re supposed to have the vertex type as a template parameter so it can be stored properly:
template
class vertex {
private:
T comp[NumAxes];
};
In which case, there’s no need for a virtual method since you can just use C++’s typecasting to do the work:
template
class vertex {
public:
template
void setComp(size_t index, U value) { comp[index] = static_cast(value); }
private:
T comp[NumAxes];
};
Now, if you want it to be virtual because you want subclasses to be able to mess with things (e.g. log every change in value), you need to define a non-templated function:
template
class vertex {
public:
template
void setComp(size_t index, U value)
{ _setComp(index, static_cast(value)); }
protected:
T comp[NumAxes];
virtual void _setComp(size_t index, T value)
{ comp[index] = value; }
};
template
class logged_vertex: public vertex {
protected:
virtual void _setComp(size_t index, T value);
};
template
void logged_vertex::_setComp(size_t index, T value)
{ cout