I have a class holding a pointer to a templated class:
[
class A
{
protected:
Feature<Base>* m_p_feature;
};

now I want to derive A with some derivative of Base:
class B : public A
{
public:
B()
{
m_p_feature = new Feature<Derived>();
}

};
where Derived is Derived from Base
]

C++ won't allow this. Why?

C++ won't allow this. Why?

Well, if you have touched upon the subject of templates in C++, then you must know that C++ templates are type-specific. This means that once you have declared a pointer(well frankly, any instantiation) to a templated class, using a certain <class> or <typename>, you will have to stick to it, no matter what. This is because C++ templates were designed to be type-safe.

So, you will have to change the constructor statement in your derived class, ClassB to m_p_feature = new Feature<Base>();. If you would like to have the templated class of type <Derived>, then you would have to make another instantiation of the templated class with this template parameter. Many programmers find this strict type-specific behaviour of templates to be annoying, as it can lead to code-bloating.

Hope this helped!

Note: Next time you post some code, wrap it within CODE-tags, with the appropriate language tags (here, cplusplus).

First of all let me simplify your code (and remove unnesasarily added inheritence):
So now, have a look at the snippet below

#include<iostream>

template<typename T>
class tc{};//a template class

class Base{};//a base class
class Derived : public Base { void der_fct(); /* ... */ };//a derived class
int main()
{
tc<Base>* ptc;//pointer ptc is pointer to tc<Base>
ptc= new tc<Derived>;//error

Base* pb;//pointer to Base
pb=new Derived;//Okay
}

Hence, you discovered that you can assign the address of a Derived object to pointer to the Base object. But you cannot assing the address of a tc<Derived> object to the pointer to the tc<Base> .
This is perfectly desirable.
Realize that "Derived is a child class of Base but tc<Derived> is not the child class of tc<Base> ."
Repeat the last quoted sentence 10 times.

Hope the above helped.

Similarly, for your code, realize that " Feature<Base> is not a base class for Feature<Derived> although Base is a base class of Derived> "

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.