Good morning,
I have a templatized class and I want to manipulate objects with a pointer vector. To use a pointer vector for a template class, I need this class, which must be derived from an unclassified class, and I did it.
Here is my problem: call the derived class method from the pointer to the base class, I can not use virtual functions, because template functions cannot be made virtual. I need to make an explicit conversion, which is tedious: if you create a numerical object with a new one, in fact, you need to make it down to number *, although it is known that the object is a number in advance.
I solved this problem awkwardly: the myset function checks all supported typeid values ββto get the correct dynamic translation. This is a long series of nested ifs that do type checking.
In addition to tediousness, the function works only to call the "set" method, and similar functions must be defined to call other methods. If I could do an automatic casting on the object that I am pointing to, everything will be much simpler.
Disadvantages of the approach:
- the code repeats: if I defined another function (for example, T get () {return val;}, I would need another myget function with a full set of nested ifs!
- the list of supported types should be explicitly defined by nesting if calls
- code may be inefficient
The compiler knows that lst [0] (in the code below) points to a numerical object, although lst is a vector of objects of the element from which the number <> of objects is deduced.
Is there a way to automatically lower a pointer, defined as base *, to a pointer to an object actually pointed to?
If I had the correct downgrade, then I could define thousands of methods in the class number <> and call them with a call to the wellcasting function β (...)
Here's the code (it works correctly, the kernel is the definition of "myset")
Thanks in advance, Pietro M.
PS I am most interested in the standard approach in C ++ without using libraries other than STL, such as Boost.
#include <iostream>