how would I do this

Started by
4 comments, last by game mercenary 18 years, 10 months ago
I have an aray of a sructs I made, I need some to have a handle function. how would I have one function do another one? what sould I use for it? if pointers are the only way I will do it, but I would rather another way. if I didnt make my self clear enough here is what I need to do

int handle(),func();
handle() = func();

so when I execute handle(); it will go to func()
Advertisement
Well, function pointers are the only way.
Its not that hard; you can just use a typedef to make it easier.
its syntax is a bit cryptic, but once you understand it its pretty easy.
assuming you have this function:
bool some_func(int x){if(x>4){return true;}else{return false;}}

you do this:
typedef bool(*name)(int);
the bool there is the return type of the fn to be pointed at,
the 'name' there is the typedef name,
and the int there is the argument list(so you could also do (int,int) if there were two ints as args instead of one).
then you would do:
name p;
p=some_func;//notice no ()
then to call it you would do:
p(someint);
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
Quote:Original post by supercoder74
Well, function pointers are the only way.
There are a few other ways. (This particular one uses a function pointer, actually, but you could use something similar and make the function call it makes hardcoded.) You could also have functions which call to another function. For example (this will only work with binary functions [functions that take two arguments] but it should give you an idea of what I mean):
template<typename ResultType, typename Arg1, typename Arg2>class IndirectFunction {    typedef ResultType (*FunctionType)(Arg1, Arg2)    FunctionType function;public:    IndirectFunction(FunctionType function) : function(function) { }    ResultType operator()(Arg1 arg1, Arg2 arg2) {        return function(arg1, arg2);    }};// Usage example:#include <iostream>int add(int x, int y) { return x + y; }int main() {    IndirectFunction<int, int, int> a(add);    std::cout << "1 + 1 = " << a(1, 1) << std::endl;    return 0;}
No, there are other solutions than function pointers.

The one I use when I don't want to mess with native function pointers is called function objects, which are essentially classes that behave like functions.

For example:

// the short name for 'function object' is 'functor'class FunctorExample{public:     const int operator()(int Data) const { return Data + 5; }};// to use the above functor, you must instantiate the class:FunctorExample functor;// and to call it:functor();


But, as that code stands, it doesn't solve your initial problem. To rememdy this, I have usually created a base interface for functors:

class IFunctor{public:     virtual void operator()(boost::any Arg) = 0;};class DerivedFunctor : public IFunctor{public:     virtual void operator()(boost::any Arg) { /* do whatever here */ }};IFunctor* functor = new DerivedFunctor;// ...functor(); // this will call DerivedFunctor::operator ()


Edit: If you don't want to go to all the trouble of functors (which is understandeable), then perhaps look at the Boost.Function library. Just [google] for Boost C++ Libraries.

Edit2: Darn, Roboguy pretty much summed it up.

I hope this helps,
nilkn
I will look through this when I have time (I am going to watch LOTR).
a function pointer looks like my best option, thanks.

This topic is closed to new replies.

Advertisement