473,385 Members | 1,492 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,385 software developers and data experts.

Method pointers / callbacks

Hi!

I'm trying to make a button class for an SDL app. The button should be
able to store a callback to a member function of another class. For that
purpose I implemented a template class named CallBack<Class, ReturnType>
with operator() which works fine.
My problem now is the usage of CallBack in the button.
I want to create the button like this:

CallBack<MyClass, void> *cb = new CallBack(this, MyClass::buttonClick);
Button *my_button = new Button("This is a button", x, y, cb);

How do I tell the button to take ANY type of callback? I don't want to
make the button class a template class...

Thanks for the help,
Martin
Jul 23 '05 #1
8 1481
Martin Höller wrote:
Hi!

I'm trying to make a button class for an SDL app. The button should be
able to store a callback to a member function of another class. For that
purpose I implemented a template class named CallBack<Class, ReturnType>
with operator() which works fine.
My problem now is the usage of CallBack in the button.
I want to create the button like this:

CallBack<MyClass, void> *cb = new CallBack(this, MyClass::buttonClick);
Button *my_button = new Button("This is a button", x, y, cb);

How do I tell the button to take ANY type of callback? I don't want to
make the button class a template class...


You could make CallBack a template, derived from an abstract base class.
Jul 23 '05 #2
Hi Martin,

I wrote a sample for your problem. I hope it helps. The Button
class needs to know only the interface (pure virtual function) of base
class of the callback.

#include <iostream>
#include <string>

using namespace std;

class CBaseCallBack
{
public:
virtual int ProcessCallBack(int x, int y) = 0;
};

template<class TYPE1, class TYPE2>
class CCallback : public CBaseCallBack
{
public:
int ProcessCallBack(int x, int y)
{
cout << "\nx = " << x << "y = " << y << endl;
// process the event here
return 0;
}

private:
TYPE1 Var1;
TYPE2 Var2;
};
class CButton
{
public:
CButton(void *pCallBackObject) { m_pCallBackObject = pCallBackObject;
}
~CButton() { };
int OnEvent()
{
((CBaseCallBack*)m_pCallBackObject)->ProcessCallBack(10, 12);
return 0;
}

private:
void* m_pCallBackObject;
};

int main()
{
CCallback<void*,void*> clsCallBack;
CButton clsTestButton(&clsCallBack);
clsTestButton.OnEvent();
return 0;
}

Bhanu Gogineni.

Martin Höller wrote:
Hi!

I'm trying to make a button class for an SDL app. The button should be
able to store a callback to a member function of another class. For that
purpose I implemented a template class named CallBack<Class, ReturnType>
with operator() which works fine.
My problem now is the usage of CallBack in the button.
I want to create the button like this:

CallBack<MyClass, void> *cb = new CallBack(this, MyClass::buttonClick);
Button *my_button = new Button("This is a button", x, y, cb);

How do I tell the button to take ANY type of callback? I don't want to
make the button class a template class...

Thanks for the help,
Martin


Jul 23 '05 #3
bhanu wrote:
Hi Martin,

I wrote a sample for your problem. I hope it helps. The Button
class needs to know only the interface (pure virtual function) of base
class of the callback.


Hi!

Thanks for your answer but this is not exactly what I'm looking for. I
want to be able to assign any member function to the callback. Your
solution works perfectly if you have to do the same thing everytime the
button is clicked. I want an all-purpose button class.

Martin
Jul 23 '05 #4
On Sun, 12 Jun 2005 19:00:57 +0400, Martin Höller
<mh******@sbox.tugraz.at> wrote:

[]
How do I tell the button to take ANY type of callback? I don't want to
make the button class a template class...


Just use boost::function as a callback type. Refer to
http://boost.org/doc/html/function.html

--
Maxim Yegorushkin <fi****************@gmail.com>
X-No-Acknowledgement: yes
X-Replace-Address: yes
Jul 23 '05 #5
In my example I have only one interface to the call back, if you need
more
inferfaces you have to declare them as pure virtual functions in the
base
class.

event flow is something like this:
some object <- call back object <- button object.

you can pass any member function to the call back object as void*, you
can
call it from the call back class, with proper arguments.

In your case, calling "any" function is not possible, without know
about the
the class. you need some abstaction if you want to make it generic.

Regards,

Bhanu Gogineni.

Martin Höller wrote:
bhanu wrote:
Hi Martin,

I wrote a sample for your problem. I hope it helps. The Button
class needs to know only the interface (pure virtual function) of base
class of the callback.


Hi!

Thanks for your answer but this is not exactly what I'm looking for. I
want to be able to assign any member function to the callback. Your
solution works perfectly if you have to do the same thing everytime the
button is clicked. I want an all-purpose button class.

Martin


Jul 23 '05 #6
In my example I have only one interface to the call back,
if you need more inferfaces you have to declare them as
pure virtual functions in the base class.

event flow is something like this:
some object <- call back object <- button object.

you can pass any member function to the call back object
as void*, you can call it from the call back class, with
proper arguments.

In your case, calling "any" function is not possible, without
knowing about the the class. you need some abstaction if you
want to make it generic.

Regards,

Bhanu Gogineni.

Martin Höller wrote:
bhanu wrote:
Hi Martin,

I wrote a sample for your problem. I hope it helps. The Button
class needs to know only the interface (pure virtual function) of base
class of the callback.


Hi!

Thanks for your answer but this is not exactly what I'm looking for. I
want to be able to assign any member function to the callback. Your
solution works perfectly if you have to do the same thing everytime the
button is clicked. I want an all-purpose button class.

Martin


Jul 23 '05 #7
Hi!

Thanks for all your answers. I found a suitable solution for my problem
with the help of your postings.
First I introduced a base callback class:

class BaseCallBack
{
public:
virtual void operator()() = 0;
};

Then I derived the main class and removed the return type template
parameter (which reduces the flexibility but is ok for my program):

template <class Class>
class CallBack : public BaseCallBack
{
public:
typedef void (Class::*Method)();

CallBack(Class* class_instance, Method method) :
class_instance_(class_instance), method_(method)
{
}

void operator()()
{
return (class_instance_->*method_)();
}

private:
Class *class_instance_;
Method method_;
};
The button class stores a BaseCallBack* which is handed over to the
constructor. So creating a button works like this:

CallBack<MyClass> *cb = new CallBack<MyClass>(this, &MyClass::foo);
button = new Button([...whatever...], cb);
Thanks again for your help!
Martin
Jul 23 '05 #8
On Sun, 12 Jun 2005 20:55:20 +0400, Martin Höller
<mh******@sbox.tugraz.at> wrote:

[]
The button class stores a BaseCallBack* which is handed over to the
constructor. So creating a button works like this:

CallBack<MyClass> *cb = new CallBack<MyClass>(this, &MyClass::foo);
button = new Button([...whatever...], cb);


boost::function would save you from writing all the boilerplate code.

boost::function<void()> cb = boost::bind(&MyClass::foo, this);

--
Maxim Yegorushkin <fi****************@gmail.com>
Jul 23 '05 #9

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

Similar topics

4
by: Brian | last post by:
Hi all, I am implementing an object that is currently using function pointers for callbacks to let me do something on assignment: template <class T> class MyClass{ private: typedef T& (*...
3
by: lallous | last post by:
Hello #include <iostream> #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember)) class Fred; typedef int (Fred::*FredMemFn)(char x, float y); class Fred {
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
11
by: Seven Kast USA | last post by:
hi What is use of pointerrs in c programming.is fasster than other types by KAST
23
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...
11
by: Protoman | last post by:
Which is more efficient for stuff like callbacks, selecting a funct from a list, and other stuff, function objects or function pointers? Thanks!!!
2
by: Bart Simpson | last post by:
I have written a library in ('native') C++, and I have made a lot of use of function pointers e.g.: /* Example C++ callbacks: typedef void (*CBFUNC1)(unsigned int, const char*, object&);...
2
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...
15
by: Christian Christmann | last post by:
Hi, in which situations pointers to functions might be more efficient/convenient than a direct function call? In the example I've found so far, I see no advantage of using pointers to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.