#include <iostream.h>
class myClass
{
public:
myClass(void)
{
think = myFunction;
}
void (*think)(void);
void myFunction (void)
{
cout << "Hello" << endl;
}
};
void main (void)
{
myClass object;
object.think();
}
When compiling in Microsoft Visual C++ 6.0 I get the following error message:
c:\cstuff\ptofunction\pointer.cpp(8) : error C2440: '=' : cannot convert from 'void (__thiscall myClass::*)(void)' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible
Thanks in advance!
=========================
"The staff in the hand of a wizard may be more than a prop for age," -Hamá the doorward from 'Lord of the Rings' by J. R. R. Tolkien
Edited by - HimitsuMonban on January 5, 2002 3:05:59 AM
C++ pointer to member function: How?
Hey there, can anyone help me figure out why this won't work?:
I have found out through a lot of trial-and-error that the following code works:
But this still does not solve my problem of being able to have the pointer inside the class itself. Cany anyone please help me rewrite this so that I can have a pointer as a member of a class which can point to a member function of that class? Thanks in advance!
=========================
"The staff in the hand of a wizard may be more than a prop for age," -Hamá the doorward from ''''Lord of the Rings'''' by J. R. R. Tolkien
#include <iostream.h>class myClass{public: void myFunction(void) { cout << "Hello" << endl; }};typedef void (myClass::*pmFunction)(void);pmFunction pMyFunction = &myClass::myFunction;void main(){ myClass object; myClass* pObject = &object (pObject->*pMyFunction)();}
But this still does not solve my problem of being able to have the pointer inside the class itself. Cany anyone please help me rewrite this so that I can have a pointer as a member of a class which can point to a member function of that class? Thanks in advance!
=========================
"The staff in the hand of a wizard may be more than a prop for age," -Hamá the doorward from ''''Lord of the Rings'''' by J. R. R. Tolkien
In your first code:
void (*think)(void)
is a pointer to a __cdecl function (as all functions outside classes), while
void myFunction (void)
is a __thiscall function, that means that when the code is called the function is also givin a pointer to the "this" object, which is the instance of class. The compiler automaticly sets __thiscall for functions in classes
you should declare the fuction pointer as:
This is a pointer that points to a function that is a member of the myClass class.
I am not 100% sure this will work, but it probably will.
void (*think)(void)
is a pointer to a __cdecl function (as all functions outside classes), while
void myFunction (void)
is a __thiscall function, that means that when the code is called the function is also givin a pointer to the "this" object, which is the instance of class. The compiler automaticly sets __thiscall for functions in classes
you should declare the fuction pointer as:
void (__thiscall myClass::*think)(void)
This is a pointer that points to a function that is a member of the myClass class.
I am not 100% sure this will work, but it probably will.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement