Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store a function to a variable?

I think they are called functors? (it's been a while)

Basically, I want to store a pointer to a function in a variable, so I can specify what function I want to use from the command line.

all the functions return and take the same values.

unsigned int func_1 (unsigned int var1)
unsigned int func_2 (unsigned int var1)

function_pointer = either of the above?

so then I could call it by going: function_pointer(my_variable)?

EDIT: as per @larsmans's suggestion, I've gotten this: Config.h:

class Config
{
public:
    unsigned static int (*current_hash_function)(unsigned int);
};

Config.cpp:

#include "Config.h"
#include "hashes.h"
unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;

hashes.h:

unsigned int kennys_hash(unsigned int out);
unsigned int kennys_hash_16(unsigned int out);

hashes.cpp:

just implements the functions in the header

main.cpp:

#include "Config.h"
#include "hashes.h"
// in test_network:
    unsigned int hashed = Config::current_hash_function(output_binary);

//in main():
        else if (strcmp(argv[i], "-kennys_hash_16") == 0)
        {
            Config::current_hash_function = kennys_hash_16;
        }
        else if (strcmp(argv[i], "-kennys_hash_8") == 0)
        {
            Config::current_hash_function = kennys_hash;
        }

the error I get:

g++ -o hPif src/main.o src/fann_utils.o src/hashes.o src/Config.o -lfann -L/usr/local/lib 
Undefined symbols:
  "Config::current_hash_function", referenced from:
      test_network()     in main.o // the place in the code I've selected to show
      auto_test_network_with_random_data(unsigned int, unsigned int, unsigned int)in main.o
      generate_data(unsigned int, unsigned int, unsigned int)in main.o
      _main in main.o // the place in the code I've selected to show
      _main in main.o // the place in the code I've selected to show
      generate_train_file()     in fann_utils.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [hPif] Error 1
like image 861
NullVoxPopuli Avatar asked Apr 07 '11 14:04

NullVoxPopuli


People also ask

How do you store a function in a variable?

Functions stored in variables do not need function names. They are always invoked (called) using the variable name. The function above ends with a semicolon because it is a part of an executable statement.

How do you set a function as a variable?

Method 1: Assign Function Object to New Variable Name A simple way to accomplish the task is to create a new variable name g and assign the function object f to the new variable with the statement f = g. What is this? The reason is that both variables point to the same function object in memory.

How do you store functions in a variable in Python?

In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.

How do you assign a function to a variable in CPP?

In C++, assigning a function to a variable and using that variable for calling the function as many times as the user wants, increases the code reusability. Below is the syntax for the same: Syntax: C++


1 Answers

The simplest you can do is

unsigned int (*pFunc)(unsigned int) = func_1;

This is a bare function pointer, which cannot be used to point to anything other than a free function.

You can make it less painful if your compiler supports the C++0x auto keyword:

auto pFunc = func_1;

In any case, you can call the function with

unsigned int result = pFunc(100);

There are many other options that provide generality, for example:

  • You can use boost::function with any C++ compiler
  • With a compiler implementing features of C++0x you can use std::function

These can be used to point to any entity that can be invoked with the appropriate signature (it's actually objects that implement an operator() that are called functors).

Update (to address updated question)

Your immediate problem is that you attempt to use Config::current_hash_function (which you declare just fine) but fail to define it.

This defines a global static pointer to a function, unrelated to anything in class Config:

unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;

This is what you need instead:

unsigned int (*Config::current_hash_function)(unsigned int) = kennys_hash_16;
like image 189
Jon Avatar answered Sep 19 '22 12:09

Jon