Topic: Q: Why void* but no void::*x(...) ?


Author: eyala@applicom.co.il (Eyal Alaluf)
Date: Wed, 1 Feb 1995 10:03:55 GMT
Raw View
Nat Pryce (np2@doc.ic.ac.uk) wrote:
: I want to build a Timer object with which objects can schedule
: callbacks to member functions. I can pass any object to the
: Timer by casting it's address to a void*. However, I cannot
: do the same with a member-function pointer. There is no way
: of expressing void(void::*fn)(void), or whatever.

: My question is: why not? If I can break the type checking for
: object pointers why can't I break it for member function pointers?

: Any comments will be most appreciated.

If all your objects have a common base class say Bone, you can do the following:

class Bone {
...
};

typedef void (Bone::*fn) (void);

class A : public Bone {
public:
 void f (void);
};

class B {
public:
 void f (void);
 int  g (void);
};

fn anyA = (fn) &A::f; // Works...
fn anyA1 = (fn) &A::g; // Works... (checked only on Sun C++ 4.0.1)
fn anyB = (fn) &B::f; // Does'nt work...

This works on HP C++ 3.4, Sun C++ 4 and gnu 2.6.0. It did not work on gnu 2.4.3
or something.
I am not certain at all what is the standard regarding this, and I would like
it very much if there was a way to cast from any type of method to any type.
One other thing, I am not certain at all how this works with regard to
multiple inheritance, but I do not use that so I am not very concerned.
Please email eyala@applicom.co.il as well as posting.

-----------------------------------------------------------------------------
    Eyal Alaluf                                eyala@applicom.co.il
    Mainsoft Israel Ltd.                       c/o Applicom Systems Ltd.
                                               16 Abba Hillel Silver St.
    Phone : 972 -(3) 575 5550 ext : 1287       Ramat-Gan, 52506
    Fax   : 972 -(3) 751 5906                  Israel
------------------------------------------------------------------------------




Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 2 Feb 1995 11:10:24 -0600
Raw View
eyala@applicom.co.il (Eyal Alaluf) writes:

>Nat Pryce (np2@doc.ic.ac.uk) wrote:
>: I want to build a Timer object with which objects can schedule
>: callbacks to member functions. I can pass any object to the
>: Timer by casting it's address to a void*. However, I cannot
>: do the same with a member-function pointer. There is no way
>: of expressing void(void::*fn)(void), or whatever.

>: My question is: why not? If I can break the type checking for
>: object pointers why can't I break it for member function pointers?

>: Any comments will be most appreciated.

>If all your objects have a common base class say Bone, you can do the following:

>class Bone {
>...
>};

>typedef void (Bone::*fn) (void);

>class A : public Bone {
>public:
> void f (void);
>};

>class B {
>public:
> void f (void);
> int  g (void);
>};

>fn anyA = (fn) &A::f; // Works...
>fn anyA1 = (fn) &A::g; // Works... (checked only on Sun C++ 4.0.1)
>fn anyB = (fn) &B::f; // Does'nt work...

...

This is not a legal C++ cast. In C++ is is legal to convert
pointer to member (or member function) of a base class to pointer
to member of a derived class, as member of a base class are still
members of the derived class. However, the reverse conversion,
pointer to member of derived to point to member of base is not
defined. The compiler may be doing a bitwise conversion of the
pointer, which may or may not work.




Author: arnoud@ijssel.xs4all.nl (Arnoud Martens)
Date: Fri, 27 Jan 1995 20:31:51 GMT
Raw View
Arnoud Martens <arnoudm@ijssel.xs4all.nl> wrote:
>template <class T>
>class Callback{
>public:
> Callback(void (T::*function)(void), T& aT)
>    pfn(function), t(aT) {};
> void call() { (t.*fn)() };
>private:
> T t;

This should of course be T& t,

> void (T::*fn)(void);
>};


--
Name: Arnoud Martens, Utrecht, the Netherlands,  tel: +31-30-732679
E-mail: arnoudm@ijssel.xs4all.nl WWW: http://www.xs4all.nl/~arnoudm




Author: arnoud@ijssel.xs4all.nl (Arnoud Martens)
Date: Mon, 23 Jan 1995 00:26:20 GMT
Raw View
In article <3fok12$pnk@frigate.doc.ic.ac.uk>,
Nat Pryce  <np2@doc.ic.ac.uk> wrote:
>I want to build a Timer object with which objects can schedule
>callbacks to member functions. I can pass any object to the
>Timer by casting it's address to a void*. However, I cannot
>do the same with a member-function pointer. There is no way
>of expressing void(void::*fn)(void), or whatever.
>
>My question is: why not? If I can break the type checking for
>object pointers why can't I break it for member function pointers?
>

Becase void is not a class, and it does not have any member
functions. For callbacks in C you can create a Callback class
based on a template:

template <class T>
class Callback{
public:
 Callback(void (T::*function)(void), T& aT)
    pfn(function), t(aT) {};
 void call() { (t.*fn)() };
private:
 T t;
 void (T::*fn)(void);
};

If you want to generize this class for return type and parameter
you must specify extra template types.

Gtx:


--
Name: Arnoud Martens, Utrecht, the Netherlands,  tel: +31-30-732679
E-mail: arnoudm@ijssel.xs4all.nl WWW: http://www.xs4all.nl/~arnoudm




Author: stidev@gate.net (Solution Technology)
Date: 20 Jan 1995 17:47:00 GMT
Raw View
Nat Pryce (np2@doc.ic.ac.uk) wrote:
: I want to build a Timer object with which objects can schedule
: callbacks to member functions. I can pass any object to the
: Timer by casting it's address to a void*. However, I cannot
: do the same with a member-function pointer. There is no way
: of expressing void(void::*fn)(void), or whatever.

: My question is: why not? If I can break the type checking for
: object pointers why can't I break it for member function pointers?

void is not a class, so it has no member functions!
Don't know about void parameters either.
Best you can do is:    void(someclass::*f)();


Ken Walter





Author: Nat Pryce <np2@doc.ic.ac.uk>
Date: 20 Jan 1995 15:14:42 GMT
Raw View
I want to build a Timer object with which objects can schedule
callbacks to member functions. I can pass any object to the
Timer by casting it's address to a void*. However, I cannot
do the same with a member-function pointer. There is no way
of expressing void(void::*fn)(void), or whatever.

My question is: why not? If I can break the type checking for
object pointers why can't I break it for member function pointers?

Any comments will be most appreciated.

Thanks,
 Nat.
--
+------------------------------------------+---------------------------------+
| Name:   Nat Pryce MEng ACGI              | Mail:  Department of Computing, |
| Email:  np2@doc.ic.ac.uk                 |        Imperial College,        |
| Tel:    +44 (1)71 594 8934 (Direct Dial) |        180 Queen's Gate,        |
| Fax:    +44 (1)71 581 8024               |        London SW7 2BZ,          |
| WWW:    http://scorch.doc.ic.ac.uk/~np2  |        United Kingdom           |
+------------------------------------------+---------------------------------+