[C++] Cast to a class without knowing it's template?
I have a heterogeneous array of non-templated class Base, I then have a templated class Derived, it holds variables of type T. I then have a function which walks the array. Through a boolean in the Base class I know I can cast the array element to Derived, I only use other variables in Derived, not variables of type T.
I am not able, as function caller, to pass which type T is. So is there something so I don't have to care about what T is? Or can I simply cast it as Derived<void> *array_element = array[element]; or something?
Thanks
PS: in fact, the variable in Derived is a member function pointer as (T::*member_function)()
Could you use a virtual function defined in Base to delegate to Derived, which then of course knows its own T?
Quote:Original post by ApochPiQ
Could you use a virtual function defined in Base to delegate to Derived, which then of course knows its own T?
Hmm, how precisely do you have this in mind? I can't somehow return the type T. Or do you mean to do the processing inside the delegated function, instead from an outsiders function (which doesn't knows T)?
Can't I just assign any type? It doesn't matter, I'm not going to use it, but the fact it's used by a member function pointer makes me uncertain...
Quote:Original post by Decrius
I have a heterogeneous array of non-templated class Base, I then have a templated class Derived, it holds variables of type T. I then have a function which walks the array. Through a boolean in the Base class I know I can cast the array element to Derived, I only use other variables in Derived, not variables of type T.
I am not able, as function caller, to pass which type T is. So is there something so I don't have to care about what T is? Or can I simply cast it as Derived<void> *array_element = array[element]; or something?
Thanks
PS: in fact, the variable in Derived is a member function pointer as (T::*member_function)()
So let me see if I have this straight. You have something like
struct Base { bool is_derived; Base(bool is_derived = false): is_derived(is_derived) {}};template <typename T>struct Derived<T>: Base { void (T::*func)(); Derived(void (T::*func)()): func(func), Base(true) {}};std::vector<Base*> function_wrappers;for ( std::vector<Base*>::iterator it = function_wrappers.begin(), end = function_wrappers.end(); it != end; ++it) { // You want to extract the Derived instance and invoke its member function // upon a T, without knowing what type T is?}
The flag isn't helping you here. Use polymorphism, and have some way of supplying the T to invoke upon - the obvious way would be to hold it in the Derived instance (or just hold a weak pointer, if you want to "share" an existing instance).
struct Base { virtual void operator()() = 0; virtual ~Base() {}};template <typename T>struct Derived<T>: Base { void (T::*func)(); T* target; Derived(void (T::*func)(), const T& target): func(func), target(&target) {} void operator()() { (target.->func)(); }};std::vector<Base*> function_wrappers;for ( std::vector<Base*>::iterator it = function_wrappers.begin(), end = function_wrappers.end(); it != end; ++it) { (*it)();}
That's what ApochPiQ is telling you. :)
Almost Zahlman, I think it would be best represented as this:
I'm unsure if I should make a distinct between T and M, but I thought this would be handy due to inheritance (the longer I think of it, the more I think M could be T).
Second, can I not just cast to a void pointer? Like in my example? Would make it easy :P
Thanks
struct Base { virtual bool match(void *pointer) = 0; virtual ~Base() {}};template <typename T, typename M, typename P>struct Derived<T>: Base { void (M::*func)(P *); T* target; Derived(void (M::*func)(P *), const T& target): func(func), target(&target) {} bool match(void *pointer) { return (target == pointer); }};std::vector<Base*> function_wrappers;template <typename T>void some_function(T *pointer){ for ( std::vector<Base*>::iterator it = function_wrappers.begin(), end = function_wrappers.end(); it != end; ++it ) { if ((*it).match(pointer)) // I don't care about M and P { } }}
I'm unsure if I should make a distinct between T and M, but I thought this would be handy due to inheritance (the longer I think of it, the more I think M could be T).
Second, can I not just cast to a void pointer? Like in my example? Would make it easy :P
Thanks
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement