-
Function Pointers HELP!
I am trying to mess around with a kind of circular logic between two classes using a function pointer. VS 6 is not cooperating or I am using improper syntax. Can someone give me an idea what is wrong?? thanks
Code:
#ifndef ABC_H
#define ABC_H
#include <iostream.h>
#include "xyz.h"
class Abc
{
public:
int truefalse(void);
void call_xyz(void);
private:
Xyz obj_xyz;
};
#endif
#include "abc.h"
#include "xyz.h"
int Abc::truefalse(void)
{
int int_rc;
cout << "Enter a 1 or 0: ";
cin >> int_rc;
cout << endl;
return int_rc;
}
void Abc::call_xyz()
{
obj_xyz.what_you_picked(truefalse);
}
#include "abc.h"
void main()
{
Abc obj_abc;
obj_abc.call_xyz();
}
#ifndef XYZ_H
#define XYZ_H
#include <iostream.h>
class Xyz
{
public:
void what_you_picked(int (*truefalse)(void));
};
#endif
#include "xyz.h"
void Xyz::what_you_picked(int (*truefalse)(void))
{
if((*truefalse)())
{
cout << "You Picked One" << endl;
}
if(!(*truefalse)())
{
cout << "You Picked Zero" << endl;
}
}
thats the code and here is the error i get:
C:\Windows\Desktop\software eng\test\abc.cpp(17) : error C2664: 'what_you_picked' : cannot convert parameter 1 from 'int (void)' to 'int (__cdecl *)(void)'
None of the functions with this name in scope match the target type
-
experts!
someone here has gotta know this, help me out
-
Pointers to class member functions are complicated. Are you sure you're ready for it?
There was a thread a while ago, you might want to search for it.
-
i searched "function pointers" and came up with nothing...do you have the address on hand???
thank you
-
http://www.vbforums.com/showthread.p...mplate+pointer
Here it is, it's with templates so it's more complicated than what you're doing, but you may be able to get the info you need.
-
it definately gave me some good info, but im not sure if i can directly apply it to what i am trying to do.
in that example, the function pointer and class address are being passed through main, but in mine i need the Xyz class to call the function truefalse in Abc, to do this it seems it would need knowledge of the class Abc to recieve the parameters into the what_you_picked function. but i dont know how to give Xyz knowledge of class Abc.
am i making any sense here? hmmm
-
The problem is that a class member function
void abc::dosomething(int i)
{
m_i = i * 5;
}
is this for the compiler:
void dosomething(abc *this, int i)
{
this->m_i = i * 5;
}
So the compiler converts these calls:
pABC->dosomething(5);
abc_obj.dosomething(5);
to
dosomething(pABC, 5);
dosomething(&abc_obj, 5);
So the compiler always needs a class object to call any class member functions. When you have a pointer to a member function of class abc, you call it like this:
pABC->*pFunc(params);
And the typedef of the pointer is
typedef void (abc::*FUNC_PTR)(int);
So to make your program work:
Code:
#ifndef XYZ_H
#define XYZ_H
#include <iostream.h>
class abc; // forward declaration
class Xyz
{
public:
void what_you_picked(int (abc::*truefalse)(void), abc* pabc);
};
#endif
#include "xyz.h"
void Xyz::what_you_picked(int (abc::*truefalse)(void), abc* pabc)
{
if((pabc->*truefalse)())
{
cout << "You Picked One" << endl;
}
if(!(pabc->*truefalse)())
{
cout << "You Picked Zero" << endl;
}
}
void Abc::call_xyz()
{
obj_xyz.what_you_picked(truefalse, this);
}
-
so close
now it will compile and runs part way, but it gives a debug error as call_xyz returns...
something about the ESP not being properly saved across a function call, and not using the proper calling convention.
i have no idea what this might be???
-
use
int (_cdecl abc::*truefalse)(void)
It's about calling conventions - who is responsible for clearing the parameters and such.
-
if that doesn't work, use
int (_stdcall abc::*truefalse)(void)