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);
     execute(function(s));
}

any help would be appreciated
thanks in advance

Recommended Answers

All 4 Replies

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;
    else
        fn = NULL;

    if( fn != NULL)
    {
        int x = fn(1); // call the function
        cout << "x = " << x << "\n";
    }
    else
        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.

thanks for all the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.