Well, normally, you're supposed to have the vertex type as a template parameter so it can be stored properly:
template<typename T, size_t NumAxes = 3>
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<typename T, size_t NumAxes = 3>
class vertex {
public:
template <typename U>
void setComp(size_t index, U value) { comp[index] = static_cast<T>(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<typename T, size_t NumAxes = 3>
class vertex {
public:
template <typename U>
void setComp(size_t index, U value)
{ _setComp(index, static_cast<T>(value)); }
protected:
T comp[NumAxes];
virtual void _setComp(size_t index, T value)
{ comp[index] = value; }
};
template<typename T, size_t NumAxes = 3>
class logged_vertex: public vertex<T, NumAxes> {
protected:
virtual void _setComp(size_t index, T value);
};
template<typename T, size_t NumAxes = 3>
void logged_vertex<T, NumAxes>::_setComp(size_t index, T value)
{ cout << "Index " << index << " changed from " << comp[index];
vertex<T, NumAxes>::_setComp(index, value);
cout << " to " << comp[index] << endl;
}