I am trying to implement a list of data of any type by myself, and I want to print this list based on the data type. At this point, I have tested integers and characters. To print a list of these types, there are two methods print_int
and print_char
, depending on the data type being displayed. My question is, is it possible to define only one method print
that will print depending on the data type of my list without creating a class template (unlike List<type> ...
)?
I mean without this class definition:
template <class T>
class List{
T *data;
List *next;
...
-------------------------- UPDATE--------------------------
Finally, I found a solution that gives me the ability to dynamically check the type in C++ (using dynamic_cast
, see the accepted answer). Unfortunately, many people mistakenly believe that C++ is severely limited. Which demonstrates my question, to which many, not knowing the answer, put minuses.