From: "Dan Updegraff"
Subject: Re: WIN32 CALLBACK problems - PLEASE HELP
Date: 1997/11/17
Message-ID: #1/1
References:
Organization: Eastman Kodak Company
Reply-To: "Dan Updegraff"
Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.win32,fido.win32,microsoft.public.win32,microsoft.public.win32.programmer,microsoft.public.win32.programmer.kernel
It sounds like your basic problem is accessing a class method using a
pointer to it rather than the
traditional calling convention. In my experience, class methods cannot be
accessed this way unless
they are made static. Unfortunately, making a method static means it can
no longer access the
classes member variables. However, this problem is solved by using a
static "This Pointer." It requires
some work and I'm not sure it will solve your specific case, but if you are
interested here is a sample
console application that demonstrates how to access a class's method via a
pointer.
// ----------------------------------------------------------------
// StaticMethodExample.cpp:
//
// This file is an example of the steps needed to access a method
// of a class as a pointer to a function. To do this, the class
// method must be made static. However, in doing so, the static
// method no longer can access member variables of the class. To
// get around this side effect, a static "this pointer" must be setup
// which the static method can use to access the member variables.
// ----------------------------------------------------------------
#include
// ----------------------------------------------------------------
// Declare a class. (Usually in its own H File.)
class foo
{
public:
foo( void ); // Constructor.
~foo( void ); // Destructor.
static void set(int i); // Method - must be declared with 'static'.
static foo *mStaticThisPtr; // The static "This Pointer."
protected:
int mInt; // A member variable for the class to use.
};
// ----------------------------------------------------------------
// Implement the class. (Usually in its own CPP File.)
foo *foo::mStaticThisPtr = 0; // Init the static member var.
foo::foo( void ) // Constructor.
{
mStaticThisPtr = this; // Set the static member var to point to this
class.
mInt = 0;
}
foo::~foo( void ) // Destructor.
{
// Does nothing.
}
void foo::set( int i ) // Method.
{
mStaticThisPtr->mInt = i; // Assign the value to our member var.
printf( "set i = %d\n", mStaticThisPtr->mInt ); // Print the member
var.
}
// ----------------------------------------------------------------
// Implement the main program that uses the class.
// For this example, this is a console application that prints
// out the numbers 10 and 20.
typedef void FUNCTYPE(int); // FUNCTYPE is a function that takes an 'int'
// and returns a 'void'. This typedef must
// match the class method's signature you are
// trying to access.
void main( void );
void main( void )
{
foo myFoo;
// Instantiate the class and use it the normal way to
// verify things work okay.
myFoo.set(10);
// Now setup a pointer to the class's method and call the
// method using that pointer.
FUNCTYPE *myMethodPtr;
myMethodPtr = myFoo.set; // Inits the pointer.
myMethodPtr(20); // Calls the static method.
} // main
Good Luck,
-- Dan Updegraff --
|