473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Member Callback Functions

The aim of the following code is to store and call member functions on an
object.

class C {};

class CF
{
public:
template<typename T> inline CF(T& o, void (T::*f)())
: pO(&o), pF( (void (C::*)()) f) {};

inline void Call() { (pO->*pF)(); };

void (C::*pF)();
C* pO;
};

class A : public C
{
public:
void F() {};
};

int main()
{
A a;
CF f(a, A::F);
f.Call();
}

Objects of type CF will be stored in an array (or container) and may call
functions from different classes. Performance and memory comparisions with
virtual functions would depend on usage.

My question: since class C appears to have little impact other than allowing
compilation, can this be written without C? What other alternatives are
there (with type generic storage)?

Regards,
cadull
Jul 23 '05 #1
1 1555
Moving the cast up a level (to a structure containing the function pointer)
removes the need for A to extend C. This provides the interface I'm after.
Possible improvements?

class CF
{
public:
class C {};

template<typename T> class D
{
public:
inline D(T& o, void (T::*f)())
: pO(&o), pF(f) {};

void (T::*pF)();
T* pO;
};

template<typename T> inline CF(T& o, void (T::*f)())
: d( (D<C>&) D<T>(o, f)) {};

inline void Call() { (d.pO->*d.pF)(); };

D<C> d;
};

class A
{
public:
void F() {};
};

int main()
{
A a;
CF f(a, A::F);
f.Call();
}

"cadull" <cl********@spam.cadull.com> wrote in message
news:d5**********@nnrp.waia.asn.au...
The aim of the following code is to store and call member functions on an
object.

class C {};

class CF
{
public:
template<typename T> inline CF(T& o, void (T::*f)())
: pO(&o), pF( (void (C::*)()) f) {};

inline void Call() { (pO->*pF)(); };

void (C::*pF)();
C* pO;
};

class A : public C
{
public:
void F() {};
};

int main()
{
A a;
CF f(a, A::F);
f.Call();
}

Objects of type CF will be stored in an array (or container) and may call
functions from different classes. Performance and memory comparisions with
virtual functions would depend on usage.

My question: since class C appears to have little impact other than
allowing compilation, can this be written without C? What other
alternatives are there (with type generic storage)?

Regards,
cadull

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: prettysmurfed | last post by:
Hi all I have a bit of a problem, the subject of this post is almost selfexplaing. But here goes: Heres an example of the code I want to implement, its all nice and simple, but the flaw is I...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
7
by: Steven T. Hatton | last post by:
I am trying to convert some basic OpenGL code to an OO form. This is the C version of the program: http://www.opengl.org/resources/code/basics/redbook/double.c You can see what my current...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
3
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a...
1
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
6
by: JDT | last post by:
Hi, Can we pass a member function in a class as a callback function? Someone instucted me that I can only use a static functon or a global function as a callback. Your help is appreciated. JD
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.