Implicit class listing derived from base template class

I have a problem with implicit casting, templates, and inheritance from template classes. The following is what I extracted from my project, I did not notice that some classes are even abstract, but this is irrelevant.

class A {};
class B : public A {};

template <typename T> class Base {};
class Derived : public Base<B> {};

int main() {
    Derived d;
    Base<A>* base = new Derived();
}

Basically, I have a base template class Basefrom which I get Derived : public Base<B>from. Then I have to attribute it to the most general form of Base, which is Base<A>.

I would think that I can carry an object from Base<B>to Base<A>, as it Bcomes from A. Am I doing something wrong or how can I do it implicitly? This is important because I need to accept all types of Derived classes in a method Baseas a parameter.

Thanks in advance.

+5
2

. Base<A> Base<B>, A B.

+6

Base<B> Base<A>. Derived. , .

template <typename T>
class Base
{
    template <typename TOther>
    Base(const TOther& that)
    {
        // ...
    }
    // ...
};

, Base. , .

+2

All Articles