Public Group

# how would I do this

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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()

##### Share on other sites
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);

##### Share on other sites
Quote:
 Original post by supercoder74Well, 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;}

##### Share on other sites
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

##### Share on other sites
I will look through this when I have time (I am going to watch LOTR).

##### Share on other sites
a function pointer looks like my best option, thanks.

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631067
• Total Posts
2997734
×