Passing a pointer type as a template parameter
The following code
#include <iostream>
template<typename T>
class Abstract
{
public:
virtual ~Abstract()
{}
virtual void func(const T ¶m) const = 0;
};
class Concrete : public Abstract<int*>
{
public:
virtual void func(const int *¶m) const override // watch out!
{}
};
int main()
{
Concrete c;
}
creates the following compilation error on a line marked with a comment:
error C3668: "Concrete :: func": method with override specifier "override" did not override base class methods
If I remove override
this error is shown:
error C2259: "Concrete": cannot create abstract class
How to get from Abstract<int*>
?
source to share
Declare Concrete
like this:
class Concrete : public Abstract<int*>
{
public:
virtual void func(int * const ¶m) const override
{}
};
Question: what part of the parameter Abstract<int*>::f()
is equal const
?
Answer: Since T
there is const
and T
in Abstract<int*>
is a pointer (to int
), it is a pointer that const
is not int
.
source to share