Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:11:43+00:00 2026-06-02T05:11:43+00:00

I have a dispatcher which can return any type, takes a command, and a

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:11:44+00:00Added an answer on June 2, 2026 at 5:11 am

    Your FormData class is the base class, Form is derived, however your login function looks like

    void login(const Form *f){}
    

    But in your dispatch function, you are trying to pass a base-class pointer

    T dispatch(const Command command, const FormData *f)
    {
        switch (command)
        {
        case LOGIN: login(f);
        }
    

    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:

    struct FormData {};
    
    struct Form : public FormData {};
    
    void login(const Form *f){}
    
    enum Command
    {
        LOGIN
    };    
    
    template <typename T>
    T dispatch(const Command command)
    {
        return T();
    }
    
    template <typename T, typename FormDataType>
    T dispatch(const Command command, const FormDataType *f)
    {
        switch (command)
        {
        case LOGIN: login(f);
        }
    
        return dispatch(command);
    }
    
    int main()
    {
        Form f;
    
        dispatch<void>(LOGIN, &f);
        dispatch<void>(LOGIN);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTTP Request Dispatcher class that works most of the time, but
I have the view function in django that written like a dispatcher calling other
The following code throws an exception which I have no idea what goes wrong
I have an app that runs a process which needs to open an internet
I have a webapp in which I define the basic dispatcher-servlet context in web.xml
I have written a simple Java dispatcher with a daemon thread to handle the
I have the following in my dispatcher-servlet.xml <?xml version=1.0 encoding=UTF-8?> <beans xmlns=http://www.springframework.org/schema/beans xmlns:mvc=http://www.springframework.org/schema/mvc xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
I have the doubt regarding when to use the Dispatcher.Invoke to update something on
I have event manager process that dispatches events to subscribers (e.g. http_session_created, http_sesssion_destroyed). If
I have a ProgressBarWindow which has a progressbar and a cancel button on it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.