mercredi 20 décembre 2017

c++ instantenation of virtual member function

I have a base class (with which i want to simulate interfaces)

template<typename TType>
class Base
{
public:
    virtual SomeTemplatedClass<TType> GetTheObject() = 0;
}

and obviously a derived class

template<typename TType>
class Derived : public Base<TType>
{
public:
    virtual SomeTemplatedClass<TType> GetTheObject() = 0;
}

but for some specific type i have intention to specialize the 'GetTheObject'

template<> 
SomeTemplatedClass<int> Derived<int>::GetTheObject()
{
    return 5;
}

Now, Visual Studio 2015, complains it cannot instantiate abstract class, when i try to use

Derived<int>

Providing even a throwing behavior to a template version

class Derived : public Base<TType>
{
public:
    virtual SomeTemplatedClass<TType> GetTheObject() override
    {
        throw <something>;
    }
}

Let everything compile. So my question is: Why do i need to provide a generic behavior, when i have a specific one and the only one that is needed?

Aucun commentaire:

Enregistrer un commentaire