🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3454

Q: Generic Callbacks -- "Object->*func(...)"

#3454
From: brian (b.c.) white
Date: Wed, 14 Feb 1996 23:28
54 lines
2511 bytes
Does the C++ standard allow for a generic callback to be specified?

Basically, I'd like to be able to pass an arbitrary object and function of
that object to be called at some later time.  For example:

void DoIt(ANYOBJECT* Object, void (ANYOBJECT::*CallBack)( ...parmlist... ))
{
	[...]
	Object->*CallBack( ...parms... );
	[...]
}


I'd like to be able to call "DoIt" with any type of object and a pointer to
any of that object's member functions that match the 'parmlist'.  As it
stands, any object passed in would have to be in the same class hierarchy
as ANYOBJECT, thus basically requiring a unified class hierachy in order to
do this genericly.

Here is a more complex example that actually stores the object and member
function pointer so it can be called at a later time:

typedef	void (ANYOBJECT::*CallBackFunc)( ...parmlist... );
ANYOBJECT*	MyClass::CallObj;
CallBackFunc	MyClass::CallFunc;

void MyClass::Set(ANYOBJECT* Object, CallBackFunc* Func)
{
	CallObj = Object;
	CallFunc= Func;
}

void MyClass::Call( ...parms... )
{
	CallObj->*Func( ...parms... );
}


Templates do not work well because the type of object to be stored could
be based on the internal state of the program and not know until run-time,
long after the actual instance of "MyClass" has been created.

So...  Is this possible?


                                        Brian
                                 ( bcwhite@bnr.ca )

-------------------------------------------------------------------------------
    In theory, theory and practice are the same.  In practice, they're not.
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID: <4fti32$p3p@bcarh8ab.bnr.ca>
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
From: "brian (b.c.) white" <bcwhite@bnr.ca>
Newsgroups: comp.std.c++
Subject: Q: Generic Callbacks -- "Object->*func(...)"
Date: 14 Feb 1996 15:28:08 PST
Organization: Bell-Northern Research Ltd.
Approved: austern@isolde.mti.sgi.com
Message-ID: <4fti32$p3p@bcarh8ab.bnr.ca>
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date:  Wed, 14 Feb 1996 20:52:50 +0000 
Content-Identifier:  Q: Generic Ca... 
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMSJwGEy4NqrwXLNJAQFCGQIAjocH85wmXe4iPy5dPOW9Bv5DmnV1f/Zg
 eEEgJe80IPbKIVc1H7vnij8c4Utxz1qX3OLnGbYeUJf9aQSwz3dAuA==
 =avqK
Originator: austern@isolde.mti.sgi.com