473,473 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 7804

<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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.