473,738 Members | 7,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

callbacks using function pointers

Hi,

I am trying to implement a callback routine using function pointers.
Basically, I am trying to avoid tying my callback invoking member to a
particular class.

Let me explain my problem with an example. The code snippet below
should help in explaining my problem better.

class A
{
public:
A();
~A();
void func();
private:
int i;
};
A::A()
{
cerr<<" In the constructor of A"<<endl;
i= 100;
}

A::~A()
{

}

void A::func()
{
cout<<"Value of the attribute is "<<i<<endl;
}

class B
{
public:

template <class T>
void callAFunction(v oid (T::*ptr)());

};

template <class T>
void B::callAFunctio n(void (T::*ptr)())
{
T *obj = new T;
obj->*ptr();
}

int main()
{
B Bobj;
Bobj.callAFunct ion(&A::func);

return 0;
}

The callAFunction of class B is expected to call the function whose
pointer (function pointer) is passed to it as an argument, but the
defintion of the function should not be tied to Class A specific
function pointer. For some reason, the compiler cribs at the statement
"obj->*ptr();"

Can anybody point out the error in this code snippet?

Nov 8 '06 #1
3 1702
dr*********@gma il.com wrote:
[..]
template <class T>
void B::callAFunctio n(void (T::*ptr)())
{
T *obj = new T;
obj->*ptr();
(obj->*ptr)();
} [..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 8 '06 #2

Victor Bazarov wrote in message ...
>dr*********@gm ail.com wrote:
>[..]
template <class T>
void B::callAFunctio n(void (T::*ptr)()){
T *obj = new T;
obj->*ptr();

(obj->*ptr)();
What do you think about adding:

delete obj;
>
>} [..]

V
--
Bob R
POVrookie
Nov 8 '06 #3
BobR wrote:
Victor Bazarov wrote in message ...
>dr*********@gma il.com wrote:
>>[..]
template <class T>
void B::callAFunctio n(void (T::*ptr)()){
T *obj = new T;
obj->*ptr();

(obj->*ptr)();

What do you think about adding:

delete obj;
Without knowing what the member function does, I would
probably say that it won't hurt, perhaps, most likely.

Of course, if it's all the same, and the function behind
the 'ptr' does not try to retain the pointer to this object,
it might actually be easier to do

(T().*ptr)();

then, instead of those two lines.
>
>>
>>} [..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 9 '06 #4

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

Similar topics

1
1914
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those structure fields in a buffer. In my managed code(i.e. in C# program), I've used a delegate for invoking callback function and I've declared the structure too. The dll fucntion is executing finely without any errors but I'm not getting any values...
5
3246
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and object members. Can anyone help? I'm writing a C++ wrapper for a fairly old programming interface to a document editing program that has no OOP whatsoever; only tons of structs. This program has different callbacks I'm supposed to implement for...
3
4306
by: Tuxpal | last post by:
What are the pros/cons of using Callback mechanism in C? (Design perspective) What are the scenarios where it can be applied ? Tuxpal
1
2355
by: glennpierce | last post by:
Hi I was wondering if anyone knows of a method to achieve the creation of events in c. currently I use a function pointer to call one callback. However, I really need to map the function pointer or event to multiple callback functions. I thought I would have functions like NewSignal, EmitSignal and ConnectSignal but I don't know how I would construct function pointers at any other time other than compile time. Each new signal would...
23
2240
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have an SDK that I have written for C/C++ and would like to port it to C# if at all possible. Basically, I've got a structure that I need to pass to the C-style DLL (i.e. no mangled names - everything is __stdcall). This structure needs
2
2383
by: Ann Huxtable | last post by:
Hi, I want to do two types of async callbacks in C#. One involves spawning a worker thread, and the other does not invlve threads: Case 1 (Span new thread) ---------------------------- class Fred { private MyParams params = null ;
2
2180
by: djcredo | last post by:
Hello, I'm using C++ with OpenGL and GLUT. But I'm having problems with callbacks. There is a function called "glutDisplayFunc" which receives function pointers as input, specifically "void (*func)(void)". I'm calling the function inside the constructor of a class called GLTestOne, and the input I am giving is to the function "void
9
3343
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
13
3049
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of virtuals in C++ leads us to want to implement a framework where those callbacks are simply overriden virtual methods in a derived class. So...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.