Skip to main content
gamedev.net
🔒 Locked

Method Pointers

Started by angry 5 replies 550+ views
Original Post
angry
angry
Im creating this method pointer class which looks in a simplified form like this:
template <typename TCLASS, typename TRET=void, typename TARG=void>
class MethodPointer
{
     private:
         typedef TRET (TCLASS::*TMETHODPOINTER)(TARG);
         TMETHODPOINTER method;
         TCLASS*        object;

     public:
         MethodPointer()
         {
             method = NULL;
             object = NULL;
         }

         MethodPointer(TCLASS* object, TMETHODPOINTER method=NULL)
         {
            this->method = method;
            this->object = object;
         }
};
this works fine with method with arguments that is not of type void. This:
class MyClass
{
    public:
       void myMethod()
       {
          // do whatever

       }
       MethodPointer<MyClass> methPtr;

       MyClass()
       {
         methPtr = MethodPointer<MyClass>(this, &MyClass:myMethod);
       }
};
This results with compiler error: Cannot convert ''void (MyClass::*)()'' to ''void (MyClass::*)(void)'' Which is extremely annoying as this works:
typedef void (MyClass::*TMETHODPOINTER)(void);
 
Eh, is my compiler simply stupid or is it supposed to be like this? Any help is greatly appreciated!
Sneftel
Sneftel
for better or for worse, type void is treated rather specially when it is a template argument to templated classes and functions. For one thing, it doesn't indicate "no argument".

Solution: A partial specialization that leaves off the second and third template arguments.


How appropriate. You fight like a cow.

[edited by - sneftel on September 9, 2003 12:18:24 AM]
antareus
antareus
Or just use Boost::Function and save yourself the grief. They support up to fifty args in the header file and give you a way to generate even more.

Plus, its header only.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Shannon Barber
Shannon Barber
You can use the standard library to that, and boost provides better implementations to handle almost all cases.

e.g.

methPtr = std::bind1st(std::mem_fun(&MyClass:myMethod), this);

And Loki provides a generalized functor to store everything and make C++ functions in first-class citizens. They''ve added a similar thing to boost, but I haven''t use it.

typedef Loki::Functor&ltvoid, Loki::NullType> voidFunctor;
void test(){}

voidFunctor vf;

vf = std::bind1st(std::mem_fun(&MyClass:myMethod), this);
vf(); //invokes above functor
vf = &test
vf(); //invokes test()

And you can copy vf around like it''s an integer.



- Magmai Kai Holmlor

Not For Rent

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
antareus
antareus
Magmai does Loki''s functor''s have an inequality operator?

It''s actually really useful for some applications and it wouldn''t be hard to code up.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Shannon Barber
Shannon Barber
I''m inclined to think that it would be a little tricky to code up. If RTTI was enabled, it would be much easier.

Loki::Functor is polymorphic
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
davepermen
davepermen
it isn''t _that_ tricky. i''ve done it by comparing their types (without RTTI, just use the virtual operator() or some other virtual function to determine to what it really belongs), and if they are of the same type, compare them by the overloaded comparison function, a.k.a. compare if its an equal behaving object. in case of method pointers that means compare the method pointers, and the this pointer. in case of normal functions compare the function pointers..

and so on..

for type-equality you don''t need rtti. only for type-inheritance info you need it.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.