Consider the following template class:
template <typename T> class Function {
public:
virtual float eval( const T &x, const T &y ) = 0;
};
Since the 'eval' function should not modify the value of the two inputs 'x' and 'y', I put them as 'const'. Then I create the following class derived from 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 cannot instantiate the class Foo1. The compiler says that the function 'eval' is not implemented. To make the compiler happy, the derived class must be as follows:
class Foo2 : public Function <float*> {
public:
Foo2() : Function <float*> () {}
virtual float eval( float* const &x, float* const &y ) { ... }
};
Foo2::eval function uses two parameters of type 'float * const' instead of 'const float *'. In other words, Foo2::eval can modify the content of the arrays 'x' and 'y'. This is not what I want.
I have 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 as in 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 this correct?
- If I want 'const float* &x' (or 'const float* x') in the derived class, what should be my template class Function?
Thank you.