[program-l] Re: Containing callback routines in C++ classes

  • From: "qubit" <lauraeaves@xxxxxxxxx>
  • To: <program-l@xxxxxxxxxxxxx>
  • Date: Mon, 12 Oct 2009 13:16:52 -0500

Hi Tim -- Some musings and a solution.
you say it works outside the function but not inside.
This appears to be a compile time error, correct?
If so, did you try only declaring the 2 member functions inside the class 
declaration and moving the function definitions outside the class after the 
class is closed?
Any compiler should save the parsing of the body of an inline member to be 
compiled after the class to avoid ambiguities, but I don't know what IDE you 
are using.
Also, did you modify the argument type to something that can handle a 
pointer to member function? Unlike global functions, these pointers are 
stored as structures that point you into the vtable and cannot be cast to a 
long -- or even a void*.

Ok I know how you can fix this.    You can declare the function as a static 
member function instead of a regular function.  Static member functions are 
functions that have no 'this' argument and are not stored in the vtable at 
all -- the only thing that makes them different from global functions is 
that they are in the scope of the class -- they are called without a calling 
object -- for example,

classname::fun(args)

But I believe that if you have code that passes it an object, that object 
will be ignored.

The more I think of it the more I think this is your solution.
HTH
Happy hacking.
--le


----- Original Message ----- 
From: "Tim Burgess" <tim@xxxxxxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Monday, October 12, 2009 12:29 PM
Subject: [program-l] Containing callback routines in C++ classes


Hi,

I've a call back routine that works fine in the main module of a Win32
application.  However, if I turn it into a public member of a class then
refer to it from within another member function of the same class, it throws
the following error at compile time:

error C2440: 'type cast' : cannot convert from 'void (__stdcall
MCP::*)(struct HMIDIIN__ *,unsigned int,unsigned long,unsigned long,unsigned
long)' to 'unsigned long'
        Conversion is a valid standard conversion, which can be performed
implicitly or by use of static_cast, C-style cast or function-style cast

The line of code that fails is:

if (!(err = midiInOpen( &myMIDIInDevice, iPortNumber, (DWORD)midiCallback,
0, CALLBACK_FUNCTION)))
{

The actual fault seems to the the reference to the MIDICallback routine - a
friend of mine has suggested that this code is being referenced before it
exists within the compiler's v-table, but he lost me at that point and
couldn't suggest a work-around that didn't involve taking MIDICallback out
of the class module (something I'm trying to avoid, as there'll be derived
code from this base class later in the project).

Any takers?

Best wishes.

Tim Burgess
Raised Bar Ltd
Phone:  +44 (0)1827 719822
Mobile:  07989 486976
Web:  http://www.raisedbar.net
Facebook:
http://www.facebook.com/pages/Atherstone-United-Kingdom/Raised-Bar-Ltd/12023
4896800

Don't forget to vote for improved access to music and music technology at

http://www.raisedbar.net/petition.htm




** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq 

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: