Author | Message | Time |
---|---|---|
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?") | May 31, 2003, 7:23 PM |
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. | May 31, 2003, 7:49 PM |
K | You're suggesting something similar to the stl priority_queue's last template argument? [code]priority_queue< int, vector<int>, my_eval_priority_class>[/code] 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. | May 31, 2003, 7:56 PM |
Eibro | No, something like how the sort algorithm works: sort(vec.begin(), vec.end(), greater); Where greater is a function object. | May 31, 2003, 8:04 PM |
Etheran | Isn't that the same way you would do it in c? | May 31, 2003, 8:07 PM |
Eibro | [quote author=Etheran link=board=5;threadid=1500;start=0#msg11258 date=1054411675] Isn't that the same way you would do it in c? [/quote]No; the syntax for passing the object is similar, but function objects can offer much more than simple functions. | May 31, 2003, 8:14 PM |
Etheran | Show me how you would assign a "function object" Edit: Nevermind, I'll just read about them. | May 31, 2003, 8:24 PM |
TheMinistered | 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. [code] int (*pt2Function) (char *, int); // C int (TMyClass::*pt2Member) (char *, int); // C++ [/code] [code] 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 [/code] See? They are pretty much the same in syntax | June 1, 2003, 4:22 PM |
Yoni | A "more C++" alternative to function pointers is virtual functions. | June 3, 2003, 8:24 AM |
indulgence | pure virtual functions are so nifty :) | June 3, 2003, 9:54 AM |
K | there we go, I knew it was something along those lines...care to share any more information, Yoni? | June 3, 2003, 4:49 PM |
Yoni | Where in C you have something like: [code]typedef void (*SomethingHappenedProc)(void); void DoSomething(SomethingHappenedProc SomethingHappened) { // Do something... SomethingHappened(); // Do something... } // Handler implementation: void SomethingHappened(void) { // Handler code... } // Usage: DoSomething(&SomethingHappened); [/code] In C++ you will have something like: [code]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(); [/code] | June 3, 2003, 5:46 PM |
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: [code]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(/*...*/); }[/code] | June 3, 2003, 6:38 PM |