Function Pointers

S

Steve

I am trying to get a handle on function pointers, trying this example
program in Visual Studio:

#include <iostream.h>

class TestingUp
{
public:
TestingUp()
{
}
TestFunc(void(*ptr)())
{
ptr();
}
};

class TestingDown
{
public:
TestingDown()
{
x = 8;
y = 25;
pfv = Display;
test.TestFunc(pfv);
}
void Display()
{
cout << "At this time the value of x is " << x << endl;
cout << "At this time the value of y is " << y << endl;
}
protected:
int x;
int y;
void (*pfv)();
TestingUp test;
};

int main()
{
TestingDown downTest;
return 0;
}

Getting the cryptic error:
error C2440: '=' : cannot convert from 'void (__thiscall
TestingDown::*)(void)' to 'void (__cdecl *)(void)'

How do I fix that? Websites on function pointer syntax and callback
use are cryptic at best. Thanks for any help that can be provided.
 
V

Victor Bazarov

Steve said:
I am trying to get a handle on function pointers, trying this example
program in Visual Studio:

Are you sure the example is as it came? Or did you change it slightly?
#include <iostream.h>

Should be

#include said:
class TestingUp
{
public:
TestingUp()
{
}
TestFunc(void(*ptr)())
{
ptr();
}
};

class TestingDown
{
public:
TestingDown()
{
x = 8;
y = 25;
pfv = Display;

This is not valid syntax for members. You have to do

pfv = &TestingDown::Display;
test.TestFunc(pfv);
}
void Display()
{
cout << "At this time the value of x is " << x << endl;
cout << "At this time the value of y is " << y << endl;
}
protected:
int x;
int y;
void (*pfv)();

This is a pointer to function. If you intend to assign a pointer to
a member function, you need to declare it a pointer to member.

void (TestingDown::*pfv)();
TestingUp test;
};

int main()
{
TestingDown downTest;
return 0;
}

Getting the cryptic error:
error C2440: '=' : cannot convert from 'void (__thiscall
TestingDown::*)(void)' to 'void (__cdecl *)(void)'

How do I fix that? Websites on function pointer syntax and callback
use are cryptic at best. Thanks for any help that can be provided.

Get a good book. Before that, look in the FAQ. You can find it here:
http://www.parashift.com/c++-faq-lite/

Victor
 
J

John Harrison

Steve said:
I am trying to get a handle on function pointers, trying this example
program in Visual Studio:
[snip]


Getting the cryptic error:
error C2440: '=' : cannot convert from 'void (__thiscall
TestingDown::*)(void)' to 'void (__cdecl *)(void)'

How do I fix that? Websites on function pointer syntax and callback
use are cryptic at best. Thanks for any help that can be provided.

You should understand that pointers to member functions and pointers to
ordinary functions are two different things with two different syntaxes. All
your code seems to be dealing with pointers to ordinary functions but then
you try and use it on a member function (Display), switch to an ordinary
function and I guess it will compile.

Books are generally better than websites for learning C++.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top