Maybe a weird question

This is a discussion on Maybe a weird question within the C++ Programming forums, part of the General Programming Boards category; Is it possible to have some sort of variable filled with code and then execute that variable? For example: (pseudo ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Location
    Slovenia
    Posts
    12

    Maybe a weird question

    Is it possible to have some sort of variable filled with code and then execute that variable?

    For example:
    (pseudo code)
    Code:
    executablevariable ev;
    
    ev="if(i=1";
    string.append(ev," && j=1");
    string.append(ev," && k=1);");
    
    //now we execute the "variable"
    execute(ev);
    //and this line is executed:
    // if(i=1 && j=1 && k=1);
    Maybe a better example would be the SQL language where you can build the SQL sentences and then execute them.

    Is something like this possible in C++?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Yes, you could use a function pointer.

    [edit]
    Here's an example:
    Code:
    void move_up() {}
    void move_down() {}
    
    void (*fp)();
    
    int main() {
        if(key == UP) fp = move_up;
        else fp = move_down;
    
        // ...
    
        fp();
    
        return 0;
    }
    [/edit]

    [edit=2] Here's a tutorial: http://www.cprogramming.com/tutorial...-pointers.html [/edit]
    Last edited by dwks; 08-24-2006 at 02:41 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    I don't think a function pointer comes anywhere close to what the OP is asking. But it's the closest as it gets.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Yeah, C++ doesn't have anything like Perl's eval or LISP and Ruby's code passing because it's a compiled language, not an interpreted language.

    Just out of curiousity, what are you trying to do?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is it possible to have some sort of variable filled with code and then execute that variable?
    Yes.

    >Is something like this possible in C++?
    Yes.

    Here's some advice. Don't ask a yes or no question when you really want to know how to do something. We're logical and sarcastic enough to give you exactly what you ask for. To answer your intended question, it's possible, but extremely difficult. You're basically looking at a full C++ interpreter.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    You can embed interpreters for scripting languages like Perl.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    In C++, you mean? Yes, you can. But it might be too involved for the OP's purposes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Is it possible to have some sort of variable filled with code and then execute that variable?
    There should be machine code in that variable to be executable directly, not C++ code.

    [edit]
    I think the variable should be sequential in memory. Like an array.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Registered User
    Join Date
    Aug 2006
    Location
    Slovenia
    Posts
    12
    I have a windows dialog app. There are 5 checkboxes and a button. When you press the button it reads which checkboxes were selected and there is a different procedure for each possible combination.

    So I thought building dynamic code would be the best thing to do. Otherwise it's "if else" mania.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You could have a vector of function pointers.

    [edit] Or, if all the functions do is print something, you could call them when the buttons are clicked on, concatenating the output into a string:
    Code:
    output += function();
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    For five checkbox there will be 32 combinations. What kind of work will be done for each one?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    do your functions that should be called in case the button is pressed all accept different arguments?
    or do the arguments (and also the return type of the functions youre calling) all match?

    edit: so thats the signature i'm talking about - not sure if you're familiar with that word, so i put it that way.

    edit2: so in case the signatures of all your functions are identical:
    e.g.
    Code:
    void my_error_func(int x, int y); // function to handle invalid combinations of buttons
    void my_func1(int x, float y);
    void my_func2(int x, float y);
    void my_func3(int x, float y);
    void my_func4(int x, float y);
    etc...

    you could do something like:
    Code:
    void *my_function_pointer_array[32](int, float) = { my_func1, my_error_func, my_func2, my_error_fun, my_func3, my_func4, ... }
    and when the button is clicked:

    Code:
    int pattern = (is_button_enabled(FIRST_BUTTON_ID) << 0) |
                               (is_button_enabled(SECOND_BUTTON_ID) << 1) |
                         ... | (is_button_enabled(FIFTH_BUTTON_ID) << 4);
    and invoke the appropriate function by:
    Code:
    my_function_pointer_array[pattern](my_first_argument, my_other_argument);
    Last edited by Raven Arkadon; 08-24-2006 at 04:55 PM.
    signature under construction

  13. #13
    Registered User
    Join Date
    Aug 2006
    Location
    Slovenia
    Posts
    12
    No I don't think that will work in my case. It's a simple problem and as always those are the hardest.

    I have a CListCtrl and the function of searching for records. The record has five columns and the checkboxes are for which column you want to search for (there are of course also five edit boxes to input what you are searching for).

    So basically I need to build a giant if sentence to compare different columns with the user input.

    But maybe I can solve this by putting something like "kaoghasergsjng" in the strings that the user has not checked and it will surely find no match. Then I check for hits with something like:
    Code:
    hits=0;
    if(edit1==column1)
    { hits++; }
    if(edit2==column2)
    { hits++; }
    if(edit3==column3)
    { hits++; }
    if(edit4==column4)
    { hits++; }
    if(edit5==column5
    { hits++; }
    // and then if the user has checked three checkboxes hits should equal 3 if there was a hit. Hmm...

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Like this perhaps
    Code:
    // This you fill in from the dialog
    // set anyValue to true if you want to ignore a field
    // set anyValue to false if you want to compare a field
    struct {
      bool        anyValue;
      std::string search;
    } filter[5];
    
    // This is an example record containing 5 fields of strings
    // Fill this in with each row of your database
    std::string record[5];
    
    // Compare each record field with the filter
    // The loop will exit early if matching fails at some point
    matched = true;
    for ( i = 0 ; matched && i < 5 ; i++ ) {
      matched = matched && ( filter[i].anyValue || 
                             filter[i].search == record[i]
                           );
    }
    
    // if matched is true here, then
    // the record has passed the tests.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Aug 2006
    Location
    Slovenia
    Posts
    12
    Thanks but I already got it to work.

    Now I have another little problem. If the list is too long and the scroll bar appears it does not scroll down to the record that was found. How do I do that?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A weird question.
    By IanKoro in forum C Programming
    Replies: 19
    Last Post: 06-08-2009, 12:00 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Weird Question maybe
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2001, 06:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21