473,387 Members | 3,820 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,387 software developers and data experts.

A trick to wrap the callbacks ???

I'm trying to write a class for a simple openGL GUI.
I've written a method reshape:

------------------------
void MHwindow::reshape(int w, int h)
{
glViewport ( 0, 0, w, h );
[...]
glLoadIdentity ( );
}
------------------------

In the client code, this should be passed to GLUT, but

------------------------
MHwindow MainWindow();
[...]
glutReshapeFunc ( MainWindow.reshape );
------------------------

don't work:
main.cpp argument of type `void (MHwindow::)(int, int)' does not match
`void (*)(int, int)'

However, if in the client code I write:

------------------------
void reshape(int w, int h)
{
MainWindow.reshape(w,h);
}

[...]

glutReshapeFunc ( reshape );
------------------------

it work.

It's legal?
This things is very important for my project.

Thanks,

Manuel

Dec 31 '05 #1
8 1990
Manuel wrote:
I'm trying to write a class for a simple openGL GUI.
I've written a method reshape:

------------------------
void MHwindow::reshape(int w, int h)
{
glViewport ( 0, 0, w, h );
[...]
glLoadIdentity ( );
}
------------------------

In the client code, this should be passed to GLUT, but

------------------------
MHwindow MainWindow();
[...]
glutReshapeFunc ( MainWindow.reshape );
------------------------

don't work:
main.cpp argument of type `void (MHwindow::)(int, int)' does not match
`void (*)(int, int)'

However, if in the client code I write:

------------------------
void reshape(int w, int h)
{
MainWindow.reshape(w,h);
}

[...]

glutReshapeFunc ( reshape );
------------------------

it work.

It's legal?
This things is very important for my project.

Thanks,

Manuel


A member function pointer isn't at all the same as a normal function
pointer. See the FAQ (there is an entire section on this topic):
http://www.parashift.com/c++-faq-lit...o-members.html
Dec 31 '05 #2
W Marsh wrote:
A member function pointer isn't at all the same as a normal function
pointer. See the FAQ (there is an entire section on this topic):
http://www.parashift.com/c++-faq-lit...o-members.html


Thanks, I'm going to read.
However, my english is very poor, so I need a lot of time to fully
understand the details of an english text and I'm a C++ newbie too. :-(
Can you, briefly, tell me if it's legal?
If yes, I'll be more motivated to read all...

Thanks again,

Manuel

Dec 31 '05 #3
Manuel wrote:
W Marsh wrote:
A member function pointer isn't at all the same as a normal function
pointer. See the FAQ (there is an entire section on this topic):
http://www.parashift.com/c++-faq-lit...o-members.html

Thanks, I'm going to read.
However, my english is very poor, so I need a lot of time to fully
understand the details of an english text and I'm a C++ newbie too. :-(
Can you, briefly, tell me if it's legal?


Legal? I would say no, as what you are attempting to do won't even compile.

You could use a /static/ member function in this way - but as far as I
know it won't be definitely portable (and you won't be able to modify
instance data of your class - it will essentially limit you as much as
your non-member function solution).

You are going to have to design around this. The FAQ suggests wrapping
the object's member function in a non-member function. See if you can
get your head around this portion of the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-33.2
Dec 31 '05 #4
Manuel wrote:
W Marsh wrote:
A member function pointer isn't at all the same as a normal function
pointer. See the FAQ (there is an entire section on this topic):
http://www.parashift.com/c++-faq-lit...o-members.html


I'm reading...
It seem it's legal:

---------------------------------------------------

Because a member function is meaningless without an object to invoke it
on, you can't do this directly (if The X Window System was rewritten in
C++, it would probably pass references to objects around, not just
pointers to functions; naturally the objects would embody the required
function and probably a whole lot more).

As a patch for existing software, use a top-level (non-member) function
as a wrapper which takes an object obtained through some other
technique. Depending on the routine you're calling, this "other
technique" might be trivial or might require a little work on your part.
The system call that starts a thread, for example, might require you to
pass a function pointer along with a void*, so you can pass the object
pointer in the void*. Many real-time operating systems do something
similar for the function that starts a new task. Worst case you could
store the object pointer in a global variable; this might be required
for Unix signal handlers (but globals are, in general, undesired). In
any case, the top-level function would call the desired member function
on the object.
----------------------------------------------------

I've correctly understand?
thx,

Manuel
Dec 31 '05 #5
Manuel wrote:
Manuel wrote:
W Marsh wrote:
A member function pointer isn't at all the same as a normal function
pointer. See the FAQ (there is an entire section on this topic):
http://www.parashift.com/c++-faq-lit...o-members.html


I'm reading...
It seem it's legal:


What do you mean by "it"? Passing a member function pointer when a
top-level function pointer is expect simply won't compile, as you have
discovered. A static member function pointer /probably/ will. As the FAQ
says, a member function is meaningless without an object to invoke it on.

---------------------------------------------------

Because a member function is meaningless without an object to invoke it
on, you can't do this directly (if The X Window System was rewritten in
C++, it would probably pass references to objects around, not just
pointers to functions; naturally the objects would embody the required
function and probably a whole lot more).

As a patch for existing software, use a top-level (non-member) function
as a wrapper which takes an object obtained through some other
technique. Depending on the routine you're calling, this "other
technique" might be trivial or might require a little work on your part.
The system call that starts a thread, for example, might require you to
pass a function pointer along with a void*, so you can pass the object
pointer in the void*. Many real-time operating systems do something
similar for the function that starts a new task. Worst case you could
store the object pointer in a global variable; this might be required
for Unix signal handlers (but globals are, in general, undesired). In
any case, the top-level function would call the desired member function
on the object.
----------------------------------------------------

I've correctly understand?
thx,

Manuel

Dec 31 '05 #6
W Marsh wrote:
Legal? I would say no, as what you are attempting to do won't even compile.


It compile!
In the first post, I've written:

---------------QUOTE--------------------

[...]
However, if in the client code I write:

------------------------
void reshape(int w, int h)
{
MainWindow.reshape(w,h);
}

[...]

glutReshapeFunc ( reshape );
------------------------

it work.

---------------END QUOTE--------------------

Is this the wrapper suggested by FAQ?
I need to know if this solution is legal.

Regards,

Manuel
Dec 31 '05 #7
Manuel wrote:
W Marsh wrote:
Legal? I would say no, as what you are attempting to do won't even
compile.

It compile!
In the first post, I've written:

---------------QUOTE--------------------

[...]
However, if in the client code I write:

------------------------
void reshape(int w, int h)
{
MainWindow.reshape(w,h);
}

[...]

glutReshapeFunc ( reshape );
------------------------

it work.

---------------END QUOTE--------------------

Is this the wrapper suggested by FAQ?
I need to know if this solution is legal.

Regards,

Manuel


Yes, that is legal code - reshape is a top-level non-member function,
which is what glutReshapeFunc expects. There are no /type/ errors here,
but there may be other errors depending on the state of the MainWindow
object.
Dec 31 '05 #8
W Marsh wrote:
Yes, that is legal code - reshape is a top-level non-member function,
which is what glutReshapeFunc expects. There are no /type/ errors here,
but there may be other errors depending on the state of the MainWindow
object.


Thanks!
Of course, errors in MainWindow are possible, however the important
thing is that the base idea is ok in C++ rules.
Dec 31 '05 #9

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

Similar topics

1
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a...
20
by: Doug Holton | last post by:
Is there any metaclass trick or something similar to allow anonymous code blocks? I'd like to be able to let users do something like this fictitious example: b = Button() b.OnClick =: print...
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
5
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...
4
by: pw | last post by:
Hi, I have month names (coming from a field in a table) as the column heading in an Access 97 crosstab query. It is being sorted alphabetically. This will not do. The only way that I know to...
7
by: PJ6 | last post by:
After resigning myself to really learn JavaScript, I finally made my own grid control that populates itself and updates data . It's actually pretty sweet. The only problem I have with it is that...
0
by: anilkoli | last post by:
I want clear cut idea about callbacks and also of delegates I have doughts about callbacks, I feel callbacks are used for 1. recursion 2. dynamically calling a perticular method out of many...
9
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...
5
by: sherifffruitfly | last post by:
Hi, I want to use a c++ class in my c# application. Are there any clear step-by-step tutorials on an easy way to achieve this? All I've been able to find are relatively complicated discussions...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.