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 &param) const = 0;
};

class Concrete : public Abstract<int*>
{
public:
    virtual void func(const int *&param) 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*>

?

+3


source to share


2 answers


Declare Concrete

like this:

class Concrete : public Abstract<int*>
{
public:
    virtual void func(int * const &param) 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

.

+4


source


Do you want to:



virtual void func(int * const &param) const override  // watch out!

      

+1


source







All Articles