Solved

How to control c++ object externally, at runtime

Posted on 2010-09-18
1
301 Views
Last Modified: 2012-05-10
The title does not say enough. I have a program written in c++ that contains commands available to the user at run time. I'd like to allow the user to execute whatever command they want to by typing in the command. An example will make this clearer.
If I have a program that has a class A

class A
{
public:
    int Add(short a, short b) { return a+b;}
}

And the program creates the object TestA
A* TestA=new A;

I want to know:  is there a way for the user, at run time, to type in

TestA->Add(3,7)   and have it execute the actual object/function?

Obviously, the actual code is a lot more complex than this example, but I hope this makes it clearer.

Thanks, Brian

0
Comment
Question by:BrianDumas
1 Comment
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
Comment Utility
This is perfectly possible but not in a way where you can execute on the fly what they enter. You would have to decide before hand what operations your were prepared to support. You would need to parse whatever is typed into the command line, figure out what command or operation the user wishes to execute and then run it. Writing a parser can be a tricky thing but if you don't mind getting your hands dirty with a DSL (Domain Specific Language) I can highly recommend the Spirit parser that is part of Boost.

http://en.wikipedia.org/wiki/Spirit_Parser_Framework
http://boost-spirit.com/home/
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now