Since you have now figured out what you want, this is a visitor template, well, sorry, but it is. This answer shows how the dual-submit visitor template works.
I thought about using CRTP well, but this may or may not work for you, depending on the circumstances.
(Note: I used the code from the linked answer, so the names do not match, but I hope you get this idea.)
// your Z class Visitor; // superclass needed for generic handling struct Superbase{ virtual void Accept(Visitor& v) = 0; }; // your A template<class Der> class Base : public Superbase{ public: void Accept(Visitor& v){ v.Visit(static_cast<Der&>(*this)); } }; // your B class Derived1 : public Base<Derived1> { }; // new C class Derived2 : public Base<Derived1> { }; class Visitor { public: virtual void Visit(Superbase& sup){ // generic handling of any Superbase-derived type } virtual void Visit(Derived1& d1){ // handle Derived1 } virtual void Visit(Derived2& d2){ // handle Derived1 } }; int main(){ Visitor v; Derived1 d1; d1.Accept(v); }
The only problem: now you are not able to have a common descriptor for any type A , since functions cannot be both virtual and templates .: | Strike>
Clean it up, find a solution using the Superbase base class. :) It even allows you to have a Superbase container and make full use of polymorphism. :)
source share