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 3952560
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:51:54+00:00 2026-05-20T01:51:54+00:00

I’m building an application whose usage is going to look something like this: application

  • 0

I’m building an application whose usage is going to look something like this:

application --command --option1=? --option2=2?

Basically, there can be any number of options, but only one command per instance of the application. Similar to the way git works.

Now, I thought I’d write it in C++ to get some boost and stl experience and have a go with a few of those design patterns I keep reading about. So, I implemented this:

class Action
{
public:
    void AddParameter(std::string key, boost::any p);
    virtual unsigned int ExecuteAction();

protected:
    std::map<std::string, boost::any> parameters;
};

I’ll explain my logic anyway, just to check it – this is an abstract-ish action. All actions need option adding, hence the parameters map, so that we can implement at this level, but we expect ExecuteAction to be implemented by derived classes, such as my simple example DisplayHelpAction, which does pretty much what it says on the tin.

So now I’ve written a factory, like so:

class DetermineAction
{
public:
    DetermineAction();
    vx::modero::Action getAction(std::string ActionString);
private:
    std::map<std::string, vx::modero::Action> cmdmap;
};

The logic being that the constructor will create a map of possible strings you can ask for and getAction will do what it says – give it a command string and it’ll give you a class that is derived from Action which implements the desired functionality.

I’m having trouble with that constructor. I am trying this:

this->cmdmap = std::map<std::string, Action>();
this->cmdmap.insert(pair<string, Action>("help", DisplayHelpAction()));
this->cmdmap.insert(pair<string, Action>("license", DisplayLicenseAction()));

Which is causing a lot of errors. Now, I’m used to the Java Way of interfaces, so you use:

Interface I = new ConcreteClass();

and Java likes it. So that’s the sort of idea I’m trying to achieve here, because what I want do have for the implementation of getAction is this:

return this->cmdmap[ActionString];

Which should return a class derived from Action, on which I can then start adding parameters and call execute.

So, to summarise, I have two questions which are closely related:

  • Soundboard. I’m deliberately practising abstracting things, so there’s some additional complexity there, but in principle, is my approach sound? Is there an insanely obvious shortcut I’ve missed? Is there a better method I should be using?
  • How can I set up my class mapping solution so that I can return the correct class? The specific complaint is link-time and is:

    Linking CXX executable myapp
    CMakeFiles/myapp.dir/abstractcmd.cpp.o: In function `nf::Action::Action()':
    abstractcmd.cpp:(.text._ZN2vx6modero6ActionC2Ev[_ZN2vx6modero6ActionC5Ev]+0x13): undefined reference to `vtable for nf::Action'
    

Just because it might be relevant, I’m using boost::program_options for command line parsing.


Edit 1: Ok, I have now replaced Action with Action* as per Eugen’s answer and am trying to add new SomethingThatSubclassesAction to the map. I’m still getting the vtable error.

  • 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-05-20T01:51:55+00:00Added an answer on May 20, 2026 at 1:51 am
    1. One thing than needs to be said right off the bat is that runtime polymorphism works in C++ via pointers to the base class not by value. So your std::map<std::string, Action> needs to be std::map<std::string, Action*> or your derived Actions (i.e. DisplayHelpAction) will be sliced when copied into the map. Storing Action* also mean that you’ll need to explicitly take care of freeing the map values when you’re done. Note: you can use a boost::ptr_map (boost::ptr_map<std::string,Action>) (as @Fred Nurk pointed out) or a boost::shared_ptr (std::map<std::string,boost::shared_ptr<Action> >) to not worry about explicitly freeing the Action* allocated.
      The same thing about ‘Action getAction(std::string ActionString);’ it needs to become Action* getAction(std::string ActionString);.

    2. The linker error is (most likely) caused by not providing an implementation for virtual unsigned int ExecuteAction();. Also I’d say it makes sense to make it pure virtual (virtual unsigned int ExecuteAction() = 0;) – in which case you don’t need to provide an implementation for it. It will also provide the closes semantics to a Java interface for the Action class.

    3. Unless you have a very good reason for the Action derived objects to not know the entire boost:program_options I’d pass it down and let each of them access it directly instead of constructing std::map<std::string, boost::any>.

    4. I’d rename DetermineAction to something like ActionManager or ActionHandler.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.