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

  • Home
  • SEARCH
  • 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 8476959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:19:20+00:00 2026-06-10T18:19:20+00:00

I’m working on a Visual C++ 2010 Express console application. Before I go into

  • 0

I’m working on a Visual C++ 2010 Express console application.

Before I go into detail, the summary here is: How can I make an array/list/vector of functions and call them from that array?

So I’m having a little difficulty with function pointers. I’m writing a ‘Terminal’ class, which in turn has a member class ‘CommandMap’. The purpose of the CommandMap class is to store a vector/array of functions and the strings that represent them in another vector/array. I want the functions to be called (only) when the class calls them from the vector, but it executed only when I added it to the vector and not when trying to call it.

I tried defining a type for it:

typedef void (*CmdCallback)();

I declared a vector to contain them:

vector<string> CmdNames;
vector<CmdCallback> CmdFuncs;

I add them like so:

// Map a new command
bool CommandMap::Map(string name, CmdCallback func)
{
    if (!IsNullOrSpace(name) && func != NULL)
    {
        if (!Exists(name))
        {
            CmdNames.push_back(name);
            CmdFuncs.push_back(func);
            return true;
        }
    }

    return false;
}

And I try calling them like this:

// Get a command callback from its identifier
CmdCallback CommandMap::GetFunc(string name)
{
    int index = IndexOf(name);
    if (index == -1) return NULL;
    else return CmdFuncs.at(index);
}

// If the given string is a command indentifier
// it will invoke the associated callback.
bool CommandMap::Exec(string input)
{
    for each (string id in CmdStrings)
    {
        if (input == id)
        {
            CmdCallback cmd;
            cmd = GetFunc(id);
            cmd();
            return true;
        }
    }

    return false;
}

I tried using this:

CmdCallback SayHello()
{
    cout << "Hello World!" << endl;
    return NULL; // Forces me to return null, guessing since it's
                 // not 'void' but a 'void' pointer it must return something
}

int main(int argc, char *argv[])
{
    App = new Terminal(argc, argv);
    App->Commands->Map("say", SayHello);

    while (!App->ExecComplete)
    {
        App->WaitEnter();
        App->Commands->Exec("say");
        App->WaitEnter();
        App->ExecComplete = true;
    }

    return App->ExitCode;   
}

This works, at first. The function gets called when I try to Map() it though. And when I Exec() “say”, it finds the callback, but when it tries to call it, I get this runtime error, to which I can see no detail other than the option to break or continue. The code it gives me is.

I pretty much want to abandon my method and try a new approach, maybe I’m going the wrong way with the void pointer typedef, and I need to throw a ‘&’ or a ‘*’ somewhere I haven’t like in the Map() argument list. Maybe a vector isn’t the best way to do this either.

Basically, I am asking how I can I make an array of functions that can (and only) be called by referencing them from the array. I’m terrible with callbacks.

  • 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-10T18:19:22+00:00Added an answer on June 10, 2026 at 6:19 pm

    If I understand correctly you actually want to declare SayHello as void SayHello() so that a pointer to SayHello has the type void (*)() (i.e. CmdCallback) which is what you need for your vector of functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.