• Welcome to Valhalla Legends Archive.
 

Function Pointers (C++)

Started by K, May 31, 2003, 02:23 PM

Previous topic - Next topic

K

Is there a c++ equivelent of c function pointers? (read: not "how do I implement function pointers?" but "is there a better c++ way of implementing the same concept?")

Eibro

You could use function objects in a similar way to the STL: http://www.sgi.com/tech/stl/functors.html
Or use a function reference :), no wait, don't.
Eibro of Yeti Lovers.

K

#2
You're suggesting something similar to the stl priority_queue's last template argument? priority_queue< int, vector<int>, my_eval_priority_class>
I think that may not accomplish what I'm trying to do. I'm looking for a delegate-like concept, and I'd rather not use __delegate, especially since that would force me to compile as managed code.

Eibro

No, something like how the sort algorithm works: sort(vec.begin(), vec.end(), greater); Where greater is a function object.
Eibro of Yeti Lovers.

Etheran

Isn't that the same way you would do it in c?

Eibro

Quote from: Etheran on May 31, 2003, 03:07 PM
Isn't that the same way you would do it in c?
No; the syntax for passing the object is similar, but function objects can offer much more than simple functions.
Eibro of Yeti Lovers.

Etheran

#6
Show me how you would assign a "function object"
Edit: Nevermind, I'll just read about them.

TheMinistered

#7
Is there a better c++ way of implementing the /same concept/?
Well, I don't know if you would call it a better way, it's basically done the exact same way!  The only difference is the fact that C++ is object orientated.


  int (*pt2Function)         (char *, int);                  // C
  int (TMyClass::*pt2Member) (char *, int);                   // C++



class TMyClass
{
public:
   int DoSomething(char *c, int l) { return 0; };

/* more TMyClass */
};

pt2Member = TMyClass:DoSomething; // assignment
pt2Member = &TMyClass::DoSomething; // alternative using address of operator


See? They are pretty much the same in syntax

Yoni

A "more C++" alternative to function pointers is virtual functions.

indulgence

pure virtual functions are so nifty :)
<3

K

there we go, I knew it was something along those lines...care to share any more information, Yoni?

Yoni

Where in C you have something like:
typedef void (*SomethingHappenedProc)(void);
void DoSomething(SomethingHappenedProc SomethingHappened)
{
 // Do something...
   SomethingHappened();
 // Do something...
}

// Handler implementation:
void SomethingHappened(void)
{
 // Handler code...
}

// Usage:
DoSomething(&SomethingHappened);

In C++ you will have something like:
class Base
{
protected:
 virtual void SomethingHappened() = 0; // Not necessarily pure, but it is pure in this example
 
public:
 void DoSomething();
};

void Base::DoSomething()
{
 // Do something...
   SomethingHappened();
 // Do something...
}

// Handler implementation:
class Derived : public Base
{
protected:
 void SomethingHappened();
};

void Derived::SomethingHappened()
{
 // Handler code...
}

// Usage:
Derived X;
X.DoSomething();

Eibro

Eh, what I suggested is not entirely different from Yoni's suggestion. They both involve virtual functions, except mine involves calling operator() (which gives the function-like syntax) instead of a function.

Consider:
class Base { public: virtual int operator() (int x, int y) const = 0; };
class Derived : public Base { public: int operator() (int x, int y) const { return x + y; };

// ...

void DoSomething(int arg, const Base& fObj)
{
    fObj(/*...*/);
}
Eibro of Yeti Lovers.