472,782 Members | 1,216 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Dynamic #define

Hi,

I currently have a application which needs to dynamically define a
function (post-compilation). In Lisp, this can be done by embedding a
function definition inside a string, and call eval(string). In C++, I
thought about using #define macros, like so:

#define some_function() int a = 3; int b = 5; cout << a + b << endl;

int main(int argc, char *argv[]) {
some_function();
return 0;
}

main() in this case would print out "8". The problem is that I don't
know the function definition before hand, and I need something like:

#define some_function() STRING

where STRING contains "int a = 3; int b = 5; cout << a + b <<
endl" (which can be parsed from a file, for example).

Does anyone know how to do this?

Thanks!
Jan 12 '08 #1
2 7655

<ji*******@gmail.comwrote in message
news:50**********************************@i12g2000 prf.googlegroups.com...
Hi,

I currently have a application which needs to dynamically define a
function (post-compilation).
[snip]

defines are read at pre-compile time and the word dynamic usually means
"changes at runtime", so a dynamic defines are not possible.
It sounds to me that you need to use function pointers or functors
where STRING contains "int a = 3; int b = 5; cout << a + b <<
endl" (which can be parsed from a file, for example).
[snip]

This sounds like you need a factory of functors

Both "design pattern factory" and "C++ functor" can be googled, so I won't
elaborate too much. Hope that helps!
Jan 12 '08 #2
In article <5043ad3d-672f-4f3d-ab9c-051562783465
@i12g2000prf.googlegroups.com>, ji*******@gmail.com says...
Hi,

I currently have a application which needs to dynamically define a
function (post-compilation). In Lisp, this can be done by embedding a
function definition inside a string, and call eval(string).
[ ... code elided ]
Does anyone know how to do this?
C++ doesn't provide any direct support for this as part of the language
itself. You have a couple of choices. One is to write a lexer/parser and
evaluate the string yourself. Another that's applicable in some
environments via non-portable code is to invoke the C++ compiler to
create a dynamic library, then dynamically link to that library. The
details of that will depend on your specific environment though.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Nancy | last post by:
Hi, When I start my PC(winXP Pro), it always says: Unknown(): Unable toload dynamic library './php_msql.dll' - The specified module could not be found. Then my Apache servre starts, load php...
5
by: K | last post by:
I have found a script online that I want to use (I am new to PHP). It creates dynamic images based on the text that you pass it. However, no matter how I try, I can't get anything other than a...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
8
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
10
by: jflash | last post by:
Hello all, I feel dumb having to ask this question in the first place, but I just can not figure it out. I am wanting to set my site up using dynamic urls (I'm assuming that's what they're...
0
by: DennieOMP | last post by:
Hello! I have a question to OpenMP. In loop parallelisation, there is the option schedule and in this, one can use static and dynamic. What is the exact difference between them? Here an example...
1
by: lumumba401 | last post by:
Hello everybody, i am asking about how to define a bidimensional dynamic array as a global variable to use as incoming variable in a function Let us see , for example in a part of a programm...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.