I have a little bit “philosophical” question. There is a class A
class A
{
}
and classes A1, A2, A3 derived from A.
class A1 : public A
{
}
class A2 : public A
{
}
class A3 : public A
{
}
and one static method processing objects A – A3. Which variant should be preferred?
A)
class Algorithms
{
//Object of derived class could be use instead of the object base class
public: void test (const A *a) {}
};
or
B)
class Algorithms
{
public:
//Templatize parameter
template <typename TType>
void (const TType *a) {}
};
In my opinion, in this case, the option a) is preferable (so the templatization is redundant…)
The option b) means that the input can be any type, that is not in any inheritance relationship to the class A.
It would be used in the case where the method test() can work with another type B
class B
{
}
and types A-A3.
Are these conclusions correct or not?
The template allows you do to the same things with unrelated classes.
You can also use adapters so you would make adapters for A and for B (unrelated) that derive from a common adapter and perform the functionality you want.
The main purpose of a template normally is to apply the same logic to primarily unrelated types because, for example, you are manipulating collections of types in some way and the algorithm relates to how you manipulate them.