Consider the following class of templates:
template <typename T> class Function { public: virtual float eval( const T &x, const T &y ) = 0; };
Since the eval function should not change the value of the two inputs "x" and "y", I put them as "const". Then I create the next class derived from the function
class Foo1 : public Function <float*> { public: Foo1() : Function <float*> () {} virtual float eval( const float* &x, const float* &y ) { ... } };
When I compile with g ++, I get the following warning:
hidden overloaded virtual function 'Function<float *>::eval' declared here: type mismatch at 1st parameter ('float *const &' vs 'const float *&') virtual float eval( const T &x, const T &y ) = 0;
And I can not instantiate the class Foo1. The compiler says that the eval function is not implemented. To make the compiler happy, the derived class should be as follows:
class Foo2 : public Function <float*> { public: Foo2() : Function <float*> () {} virtual float eval( float* const &x, float* const &y ) { ... } };
The Foo2 :: eval function uses two parameters of type "float * const" instead of "const float *". In other words, Foo2 :: eval can modify the contents of the arrays "x" and "y". This is not what I want.
I tried to change the template class "Function" as follows:
virtual float eval( T const &x, T const &y ) = 0;
But the class Foo1 still does not work, the class Foo2 works the same as in the previous case.
- So it seems that either "const T & x" or "T const & x" in the template class implies "float * const & x" in the derived class. Is it correct?
- If I want 'const float * & x' (or 'const float * x') in a derived class, what should my Template function class be?
Thanks.