I have a dispatcher which can return any type, takes a command, and a FormData object. The idea is that I wish to inherit from FormData when passing around specific stuff.
struct FormData {};
struct Form : FormData {};
void login(const Form *f){}
enum Command
{
LOGIN
};
template <typename T>
T dispatch(const Command command, const FormData *f)
{
switch (command)
{
case LOGIN: login(f);
}
return T();
}
int main()
{
Form f;
dispatch<void>(LOGIN, &f);
return 0;
}
I get an error saying cannot convert from Form to FormData. I take away the template, everything works fine (but I need the template)
Your
FormDataclass is the base class,Formis derived, however your login function looks likeBut in your dispatch function, you are trying to pass a base-class pointer
C++ simply won’t let you do that. Form* can be implicitly converted to a FormData*, but not the other way around.
Perhaps you could add another template parameter to the dispatch function, and let that function figure out the concrete type at compile time: