Andras Balogh | 4 Jun 23:00 2003
Picon

pointer-to-member?

Hi! Using C++, I'd like to call an object's member function without
knowing the type of the object. Of course the function type and a
pointer to the object is known. I mean I'd like to pass the function's
pointer somehow, so that another function can call it, using a pointer
to the object and the pointer to the member function. Is it possible
to do that? How?

I've been looking at pointer-to-member operators, but this seems to
require the type of the object (ie. the class). What is this
pointer-to-member stuff is good for anyway?? Every example code I see
does something like this: 

class Base
{
public:
        void Print() {printf("Test\n");}
};

void (Base ::* f)() = &Base :: Print;

void main()
{
    Base *b = new Base;
    (b->*f)();
}

This is all very nice, but what is the point? We know the type of the
object, why not simply use "b->Print();" instead???

Thanks,
(Continue reading)

Jon Watte | 4 Jun 23:15 2003

RE: pointer-to-member?


You can define an interface for the kind of function you want to 
call. This interface would be abstract, and all objects actually 
implementing the function would implement this interface. Then 
you actually know "enough" about the object to call the function 
using pointer-to-member-function syntax, which is what C++ needs 
to work right.

If you cast object to void or similar, you actually lose one piece 
of information the compiler needs to call a member function: the 
specific "this" pointer offset for the interface. Thus, you have 
to pass SOME type with the known function around -- the interface.

If you can't even do this, then look into some kind of dynamic 
functor binding. Pass in an object reference which has a virtual 
function call operator, and have that object resolve the actual 
type of the object. This could be a template. You'd end up 
implementing what the compiler does for you in the interface case, 
though :-)

Cheers,

				/ h+

> -----Original Message-----
> From: sweng-gamedev-midnightryder.com-admin <at> lists.midnightryder.com
> [mailto:sweng-gamedev-midnightryder.com-admin <at> lists.midnightryder.com]On
> Behalf Of Andras Balogh
> Sent: Wednesday, June 04, 2003 2:00 PM
> To: sweng-gamedev <at> midnightryder.com
(Continue reading)

Killpack, Chris | 4 Jun 23:22 2003

RE: pointer-to-member?

> seems to require the type of the object (ie. the class). What 
> is this pointer-to-member stuff is good for anyway?? Every 

When you don't know which function you need to call. You could have a
large switch table, or you could have a large table of function ptrs.
Which is where pointer to member functions is useful

class Example
{
	void			Dispatch( void* pData );

	void			FunctorA( void* pData ) const;
	void			FunctorB( void* pData ) const;
	void			FunctorC( void* pData ) const;

	typedef void (Example::*Functor)( void* pData ) const;
	static Functor	s_FunctorTable[MAX_NUM_FUNCTORS];
};

static Functor Example::s_FunctorTable[MAX_NUM_FUNCTORS] = { &FunctorA ,
&FunctorB , &FunctorC };

void Example::Dispatch( int funcIdx , void* pData )
{
	(this->*s_FunctorTable[funcIdx])( pData );
}

Example a;
a.Dispatch( 0 , pData );

(Continue reading)

Marcelo A. Camelo | 4 Jun 23:30 2003
Picon

RE: pointer-to-member?

I used to have a "Action" template class, parameterized by type
being called (and its member method). This class implemented
an abstract Action interface that declared a pure virtual 
"Call" method. 

This way, the client code could hold a reference to the abstract
Action interface and invoke its Call method without having to
know what was the actual type of the concrete Action object.

Of course, I needed one Action type for each number of 
parameters being passed. Say, Action0 for no parameters, Action1 
for one parameter, and so on.

I think this is a pretty standard C++ callback mechanism. And
I'm sure there are a lot of variations to this theme around
the web.

I'm using past tense in this post because, ever since I started 
to code my engine using the hybrid c++/python approach, the need 
for such kind of dynamic behavior from C++ has disappeared. If
it needs to be dynamic, I'd better write it in python. So much
better this way.

Cheers,

Camelo

-----Original Message-----
From: sweng-gamedev-midnightryder.com-admin <at> lists.midnightryder.com
[mailto:sweng-gamedev-midnightryder.com-admin <at> lists.midnightryder.com]
(Continue reading)

Chris Carollo | 5 Jun 00:29 2003

RE: pointer-to-member?


It's worth noting here that member function pointers are pretty scary
things that I wouldn't really recommend anyone use.

Most notably, depending on whether the class that contains the function
pointer (Example in your example) has been defined when the compiler
hits your s_FunctorTable array, it will reserve 8, 12, or 16 bytes to
it depending on whether it has a vtable and whether it's multiply 
inherited or not.

And if elsewhere in your code the Example class _has_ been defined when
it hits another Example:: member function pointer, it will reserve 16
bytes for it, "just in case".

Yes, two different member function pointer variables, of the same type, 
each having a different sizeof().  Yes, you can assign one to the other 
and either have your data trunated or only fill in the bottom of the 
pointer and leave the rest uninitailized (depending which gets assigned 
to which), which will absolutely crash if you try to call the function.

And no, the compiler won't warn you about it because it's all safe as far
as the types are concerned, at least in MSVC++ 7.0.  So personally, I'd 
steer clear of this and just use a hardcoded switch statement (for the 
below case) or use externally implemented pure virtual interfaces (for
the case of callbacks).

-Chris

> class Example
> {
(Continue reading)

Kristoffer Grönlund | 5 Jun 02:28 2003
Picon
Picon
Picon

Re: pointer-to-member?

Hi!

Have a look at boosts signal/slot library, I think it does exactly what you
are looking for.
Link: http://www.boost.org/doc/html/signals.html

// Kristoffer

Andras Balogh <bnd <at> mailbox.hu> wrote:
> Hi! Using C++, I'd like to call an object's member function without
> knowing the type of the object. Of course the function type and a
> pointer to the object is known. I mean I'd like to pass the function's
> pointer somehow, so that another function can call it, using a pointer
> to the object and the pointer to the member function. Is it possible
> to do that? How?
>
> I've been looking at pointer-to-member operators, but this seems to
> require the type of the object (ie. the class). What is this
> pointer-to-member stuff is good for anyway?? Every example code I see
> does something like this:
>
> class Base
> {
> public:
>         void Print() {printf("Test\n");}
> };
>
> void (Base ::* f)() = &Base :: Print;
>
> void main()
(Continue reading)

Andras Balogh | 5 Jun 08:51 2003
Picon

Re[2]: pointer-to-member?

It seems to deal only with functors, and standalone functions. :(
I'd need to do this for many member functions of the same object.

Thursday, June 05, 2003, 2:28:04 AM, Kristoffer Grönlund wrote:

> Hi!

> Have a look at boosts signal/slot library, I think it does exactly what you
> are looking for.
> Link: http://www.boost.org/doc/html/signals.html

> // Kristoffer

_______________________________________________
Sweng-gamedev mailing list
Sweng-gamedev <at> lists.midnightryder.com
http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com

Andras Balogh | 5 Jun 08:48 2003
Picon

Re[2]: pointer-to-member?

Wednesday, June 04, 2003, 11:15:16 PM, Jon Watte wrote:

> You can define an interface for the kind of function you want to
> call. This interface would be abstract, and all objects actually
> implementing the function would implement this interface. Then
> you actually know "enough" about the object to call the function
> using pointer-to-member-function syntax, which is what C++ needs
> to work right.

If I understand correctly, this would only work for one function only,
no? What I'd like to do, is have lots of classes with lots of
different void (*f)() type member functions and then select some
functions of some objects, and assign them to an event. So they would
all be called by a totally unrelated function.

> If you cast object to void or similar, you actually lose one piece
> of information the compiler needs to call a member function: the
> specific "this" pointer offset for the interface. Thus, you have
> to pass SOME type with the known function around -- the interface.

I don't really understand this. Every function has a real, global
address, no?. If I can get a pointer to a standalone function, then
why can't get a pointer to a member function? Member functions are
different only because they need a this ptr as the first argument, no?
The function itself knows about the class it operates on, it only
requires a pointer, which I could supply.

Something like this would be good:

typedef void (*func)(void* this_ptr);
(Continue reading)

Sergey Nenakhov | 5 Jun 09:46 2003
Picon

Re: Re[2]: pointer-to-member?

Hi,

This should work:

class Fake
{
    int unused;
};

typedef void (Fake::*function_t)();

void do_call(Fake* p, void (Fake::*f)())
{
  (p->*f)()
}

template<class F> call_objects_function(F *p,void (F::*f)())
{
  do_call((Fake*) p, (funcion_t*) f);
}

do_call function is type independent and you can call it whenever you want 
for any type of object.

This works ok for non virtual functions, didn't test it for virtual ones.

Sergey.

--

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
(Continue reading)

Nalin Savara | 5 Jun 09:58 2003
Picon

Game Development Professionals Network.

Hi,

This is Nalin Savara here.
I"ve just created a network on Ryze.com (see: http://www.ryze.com ) which is
a premier business network site that's called "GameDevPros" or the "Game
Development Professionals Network" to serve as an online meeting place for
game development professionals of all backgrounds to network, meet and
discuss and exchange ideas and opportunities and support each other in
meeting challenges.

Right now, it's somewhat small, since I've just created it; but your
participation I guess is the only thing it's waiting for.

Do check it out and become part of it.

This network has game designers, programmers, digital artists, animators,
producers, audio engineers, and management & marketing specialists as
members. This is where experts feed their minds with ideas, discussions and
contacts and discuss new gaming methods, technologies and business models.

Regards,
Nalin Savara
CEO and Technical Director,
Darksun Technologies Pvt Ltd.
http://www.darksuntech.com
nsavara <at> darksunsoftware.com

Ph: +91-9811109407

----- Original Message -----
(Continue reading)


Gmane