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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:05:12+00:00 2026-06-16T03:05:12+00:00

I need to make, for my college homework, an interpreter in C++ for a

  • 0

I need to make, for my college homework, an interpreter in C++ for a language based on functions (or commands). The interpreter has got to read an input file, extract the words (strings), generate the commands and execute them. All commands are classes which inherit from a common super-class (Command, for example), which’s got a virtual method called execute. For each word read from the input file, a command is created and stored in a vector<Command>.

So, I’m thinking of using a hashtable, whose keys are the names of the commands (strings) and whose values are some kind of objects which allow me to create an specific class (or give me access to the constructor of an specific class), to easily create the classes for each word instead of using a chain of if-else-if’s.

By now, I’m planning to create a CommandGenerator class with a virtual method called generate which returns a new Command object. The values of my commands hash table will be objects of theCommandGenerator class. So I derive from it many other subclasses for all commands, which return specific new objects derived from Command.

But, does anything like that already exist? Or is there any more elegant way to do that? Is there any kind of object that can be extracted from a class to represent it?

  • 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-16T03:05:14+00:00Added an answer on June 16, 2026 at 3:05 am

    If each command is a subclass of Command, why don’t you use a std::vector<Command*> and push pointers to instances of each subclass? Then you can iterate over the vector and call your virtual execute function.

    The closest thing you can get about placing classes in a vector is boost::fusion::vector. But can’t be filled at runtime, no use on your specific case.


    Assuming you can use C++11. If you can define commands as just a execute function, you can do something like:

    map<string, function<void()>> cmds = {
        make_pair("print1", [](){
            cout << "1" << end;
        }),
        make_pair("print2", [](){
            cout << "2" << end;
        }),
        make_pair("print3", [](){
            cout << "3" << end;
        })
    };
    

    And then put the command on a vector with:

    vector<function<void()>> list;
    list.push_back(cmds["print1"]);
    list.push_back(cmds["print1"]);
    list.push_back(cmds["print2"]);
    

    Then just execute with a loop:

    for (function<void()>& cmd : list)
        cmd();
    

    This should print 112 to screen. But if you care a lot with speed, do a lot of ifs instead.

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

Sidebar

Related Questions

me need make settings language I've got: #define kMenuSettingsHeaderrus @Настройки #define kMenuSettingsHeadereng @Settings and
I need make input field that contain phone numbers. Users can enter 1-10 numbers.
I need to make a program in C++ that must read and write text
I need to make an Android app that will take data from my college
I need to make a work to college (Simple tasks) and one of the
I have a crystal report file I need make a tiny edit in. It
I just to need make sure I've got the PDO prepare statements correctly, will
I need make some action (dump statistical data) before the Dart program ends. The
I need make all of my posts update. I use bulk upload for store,
Need to make certain Ruby strings in my program to be immutable. What is

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.