LucasS
LucasS

Reputation: 729

C++ Function Referencing with Pre-defined Parameters

This might seem like a newb question, but I am still trying to wrap my head around pointers; specifically, pointers of functions. For example, say I have two classes with some functions (represented below with some psuedocode):

ClassA {
    void mainFunction();
    unknownReturnType getFunction();
};
ClassB {
    int mainFunction(int a, int b);
    unknownReturnType getFunction(int a, int b);
};

Now say that, in another class, I want to call the getFunction() function of each class, and have the that function return a reference of the class's mainFunction() function with some pre-defined parameters. i.e., say I do the following:

ClassB B;
unknownReturnType B_Function = B.getFunction(5, 6);

B_Function should now be a "reference" (don't know if that is the correct terminology) to object B's mainFunction() with the pre-defined parameters of 5 and 6.

It's kinda hard to explain, but in the end I want to be able to go through a list of classes (the user will be picking a certain option, each option correlating to a specific class's function). Each class will have a getFunction() that returns a "reference" of some function that class has with some pre-defined parameters. Then, somehow, I want to be able to execute that referenced function when the user picks the corresponding option.

One more example that may help:

ClassA A;
int ChosenOption;

unknownReturnType Option1 = A.getFunction(10);
unknownReturnType Option2 = A.getFunction(15);

cout << "Select option 1 or 2" << endl;
cin >> ChosenOption;

if(ChosenOption == 1) Execute Option1;
else Execute Option2;

My apologies if this question is a bit confusing; I have tried to explain it the best I can.

Upvotes: 2

Views: 351

Answers (2)

Jeremy Cochoy
Jeremy Cochoy

Reputation: 2642

class A and class B 's getFunction() can't have the same return type. When you have a function pointer, you remember the types it gets and the types it gives. When you have a member function pointer, you remember also the class at witch it belong.

So, the return type of A::getFunction should be a int (A::*) () and B::getFunction should be int (B::*) (int, int).

To call Option1 and Option2, you have to use the .* operator (or the ->* operator if you create A with new). So it would be

int (A::*Option1)() = A.getFunction(10);
//...
a.*Option1();

So, using only member function pointer, you can't ue predefinex values. But Andy Prowl's answer is probably what you are looking for.

Upvotes: 0

Andy Prowl
Andy Prowl

Reputation: 126412

I think what you need/want are std::function and std::bind (or the pretty much equivalent boost::function and boost::bind, if you can't afford C++11). First of all, in order to use them, you need to:

#include <functional>

Then, you could define your classes (and their member functions) like so:

class ClassA
{
public:
    void mainFunction()
    {
        std::cout << "ClassA::mainFunction()" << std::endl;
    }

    std::function<void()> getFunction()
    {
        return std::bind(&ClassA::mainFunction, this);
    }
};

class ClassB
{
public:
    int mainFunction(int a, int b)
    {
        return (a + b);
    }

    std::function<int()> getFunction(int a, int b)
    {
        return std::bind(&ClassB::mainFunction, this, a, b);
    }
};

And inside main() you would use them like so:

int main()
{
    ClassA a;
    auto a_function = a.getFunction();
    a_function();

    ClassB b;
    auto b_function = b.getFunction(5, 6);
    std::cout << b_function();
}

Here is a live example.

Also notice that in C++11 you could use lambdas instead of boost::bind():

class ClassA { // ...

    std::function<void()> getFunction()
    {
        return [&] { return mainFunction(); };
    }
};

class ClassB
{
    // ...

    std::function<int()> getFunction(int a, int b)
    {
        return [&] { return mainFunction(a, b); };
    }
};

Here is a live example.

Upvotes: 4

Related Questions