and it looks like this:

void Functions(int s)
      s += 5;

then can i call it somehow in c, c++, or cocoa
something similar to

void DoSomething(int s)
     Function* function = myfile.readFunction(Functions);

any help would be appreciated
thanks in advance

9 Years
Discussion Span
Last Post by gergep

You need to use function pointers. Here is a simple example where you type the name of the function you want to execute at the keyboard. Since C and C++ and not interpreted languages you can not execute code that has not already been compiled in the program.

#include <iostream>
#include <string>
using namespace std;

int foo1(int a)
    return a+5;

int foo2(int a)
    return a+6;

int foo3(int a)
    return a+7;

int main()
    int (*fn)(int a) = 0;  // This is a function pointer
    std::string line;
    cout << "Enter a function name -- foo1, foo2, or foo3\n";
    getline(cin, line);
    if( line == "foo1")
        fn = foo1;
    else if( line == "foo2")
        fn = foo2;
    else if( line == "foo3" )
        fn = foo3;
        fn = NULL;

    if( fn != NULL)
        int x = fn(1); // call the function
        cout << "x = " << x << "\n";
        cout << "fn is NULL\n";


if i can't do that, is it possible to have a dll loaded at runtime and not included in the project so i can use it


Yes, the functions could be in a DLL and loaded at runtime. But the program will still have to know at compile time which set of functions it is going to call. The name of the function could come from the data file, the call GetProcAddress() to get the function pointer from the DLL.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.