TobiMcNamobi
TobiMcNamobi

Reputation: 4813

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;
}

produces the following compile error at the line marked with the comment:

error C3668: 'Concrete::func' : method with override specifier 'override' did not override any base class methods

If I remove the override, this error will be shown:

error C2259: 'Concrete' : cannot instantiate abstract class

How to derive from Abstract<int*>?

Upvotes: 3

Views: 90

Answers (2)

yizzlez
yizzlez

Reputation: 8805

You want this:

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

Upvotes: 1

TobiMcNamobi
TobiMcNamobi

Reputation: 4813

Declare Concrete like this:

class Concrete : public Abstract<int*>
{
public:
    virtual void func(int * const &param) const override
    {}
};

The question is: Which part of the parameter of Abstract<int*>::f() is const?

The answer: Since T is const and T in Abstract<int*> is a pointer (to an int), it is the pointer that is const - not the int.

Upvotes: 4

Related Questions