Can I override and use a subclass for the template type in as the super class.
I’m a Java programmer and this works well with generics however I am unfamiliar with how to do much in C++
can I use a class
template <typename T>
class A{
public:
T get(){
return t;
}
A(){
}
void set(T tt){
t=tt;
}
private:
T t;
};
And extend it with something similar to
class B:A<B>
Where subclass B is used as generic type for class A and class B requires no generics
In principle, your usage of templates is called “Curiously recurring template pattern” (CRTP) and perfectly valid.
However, be aware that Java generics and C++ templates are very different at very many points, including the limitations on the use of B in A. Derived CRTP classes are incomplete at template instantiation, so you can’t allocate any objects of them in the base class like you did. That is the reason your code doesn’t compile. The usual method is instead to cast in the accessor methods, like: