• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

Saving a function in a variable

Is it possible to save a function (not a value) in a variable, to evaluate it later???
0
Xertunus
Asked:
Xertunus
  • 7
1 Solution
 
nietodCommented:
it is possible to save a function pointer and then use the function pointer to call the function at a later time.  Also there is a more c++ way of doing this, using classes called functors.  

Continues
0
 
nietodCommented:
If you have functions that have the same signature--same calling convention, same parameters and same return values like

int Add(int x,int y)
{
   return x+y;
};

and

int Sub(int x,int y)
{
   return x - y;
};

Then you can create a pointer that can point to any of these functions.  At typedef for this pointer makes things easier, in this case it would be

typedef int (*FunPrtTyp)(int x,int y);

continues
0
 
nietodCommented:
You can declare then pointer a pointer of this type, iniitialze it to one of the functions and call the function with the pointer like

FunPtrTyp FunPtr;

if (OperationTyp == AddOperatio)
    FunPtr = &Add; // Set pointer to the add function.
   // FunPtr = Add; is also a legal syntax for this.
else
    FunPtr = ⋐ // Set pointer to the subtact function.

   *  *  *

 // Use the function pointer as follows.
   int Result = (*FunPtr)(5,3); // Does 5+3 or 5-3;
// also "FunPtr(5,3);" is a legal syntax.

continues
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nietodCommented:
Obviously the function pointer can be passed to or returned from functions aand "saved till later" just like any pointer.

Now this same thing can be accomplished in a more C++ manor using classes.  This technique is so common that the classes used in this way are given a name--"functor".  A functor could be considered any class that has virtual functions, but usually it is a class with no data members and only a single virtual function.  Often this virtual function will be operator (), but in the following example I use a function called "Operate" because I think it makes the code easier to read.  

First you need to define a base class, preferabley pure abstract, that defines the virtual function(s).  For example

class MathOperation
{
public:
    virtual int Operate(int x,int y) = 0;
};

Then from this base class derive concrete classes for the differen operations to be performed, like

class Add : pulbic MathOperation
{
public:
    virtual int Operate(int x,int y) { return x + y; };
};

class Sub: pulbic MathOperation
{
public:
    virtual int Operate(int x,int y) { return x -  y; };
};

continues
0
 
nietodCommented:
Opps those functions shoudl all have been declared const.  That makes the class much more "flexible" and useful.  Like

class MathOperation
{
public:
    virtual int Operate(int x,int y) const = 0;
};

Then from this base class derive concrete classes for the differen operations to be performed, like

class Add : pulbic MathOperation
{
public:
    virtual int Operate(int x,int y) const { return x + y; };
};

class Sub: pulbic MathOperation
{
public:
    virtual int Operate(int x,int y) const { return x -  y; };
};
0
 
nietodCommented:

Now to use the class you would declare some code that has a pointer or reference to the base class, but actually works with one of the derived class objects.  For example  you could declare a function that takes a reference to the base class, but is passed one of the derived classes,  This funciton would call the functor's virtual function to perform some sort of action, for example

int DoMath(const MathOperation &Fun)
{
   return Fun.Operate(5,3);
};

this function could be invoiked using an object of either of the derived types.  You could create the objects as locals or globals, but since the virtual function is constant and the DoMath() procedure takes a constant reference parameter, you can simply call DoMath() using an unamed tempoaray, like

int Result = DoMath(Add()); // 5+3

Obviously you can use pointers and references to these functor objects to pass them around and to save them just like function pointers.

Let me know if you have any questions.
0
 
XertunusAuthor Commented:
Quite nice, but I want to do something like this:

RightButtonFunction = ShowPopupMenu();

OnRightButton()
{
        Run(RightButtonFunction);
}

(I hope you understand what I mean)
0
 
nietodCommented:
Either approach I showed you will do that.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now