So I'm trying to learn how to use function pointers (ie to pass the address of a function to another function to execute it - like a call back). I have it working when I use it as follows:

Classes.h:

#include <iostream>
using namespace std;
class Class1
{
		public:
		void callBack( string source, void (*myFunc) (string))
		{
				cout<<"About to do call back..."<<endl;
				myFunc( source );
		}
};

Classes.cpp:

#include "Classes.h"
void thisWorks(string works )
{
		cout<<"THIS ONE WORKS"<<works<<endl;
}
int main()
{
		Class1 c1;
		c1.callBack( "TESTING", &thisWorks );
		return 0;
}

./Classes
About to do call back...
THIS ONE WORKSTESTING


However, I would like to figure out how to get it to work where I don't have any 'standalone/global' functions, just classes:

Classes.h
#include <iostream>
using namespace std;
class Class1
{
		public:
		void callBack( string source, void (*myFunc) (string))
		{
				cout<<"About to do call back..."<<endl;
				myFunc( source );
		}
};
class Class2
{
        public:
        Class2( string myString)
        {
                Class1 class1;
                class1.callBack( myString, &test );
        }
        void test( string whatever )
        {
                cout<<"Call back function called!"<<whatever<<endl;
        }
};
Classes.cpp
#include "Classes.h"
int main()
{
		Class2 class2("TESTING" );
		return 0;
}

However, when I compile it, I get:
"Classes.h", line 18: Error: Formal argument myFunc of type void(*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>) in call to Class1::callBack(std::basic_string<char, std::char_traits<char>, std::allocator<char>>, void(*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>)) is being passed void(Class2::*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>).


Based on some googling, I'm pretty sure I have to create a typedef to void of my class, but I'm a bit confused. (Note I have a much larger example, the above is to try and illustrate just the problem).

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

Dave,

I was looking at that - went over my head. Is there an easy answer to my particular example? Part of the problem seems to be that I'm INSIDE my class when I'm trying to pass another member function to the other class. In all the other examples, I'm in main() and I'm trying to pass a member of a class (rather than being inside the class itself..) sorry if I can't convey this clearly...

Winbatch

Dave,

I was looking at that - went over my head.

Mine as well, too much of the time. I can't even recall some similar stuff elseweb that I contributed to fairly recently.

Is there an easy answer to my particular example?

Not from me at the moment.

Part of the problem seems to be that I'm INSIDE my class when I'm trying to pass another member function to the other class. In all the other examples, I'm in main() and I'm trying to pass a member of a class (rather than being inside the class itself..) sorry if I can't convey this clearly...

Member functions are not like regular functions. So pointer to member functions are not like pointers to regular functions. The implicit this makes things different... or ugly... or difficult.

I may try to find that other thread. But I know I struggled with that FAQ for quite a while before I posted anything that looked useful.

Dave,

Don't lose sleep over it. But if you're up anyway, yes please find the other thread ;)

WB

[It seems my edit may have been after your reply, so I un-edited it and replied with that edit. Apologies -- continuity.]

Say what Dr. Dave?

I had trouble following your other thread too. guess I'm biting off more than I can chew.

u are doing fine, as far as i can see, just declare test as static, next while passing a function u need not give &, and pass test like Class2::test in the callback funtion.... function

u are doing fine, as far as i can see, just declare test as static, next while passing a function u need not give &, and pass test like Class2::test in the callback funtion.... function

That works, unfortunately, I don't want it to be a static function as I want it to use the instance of the class that I'm working in.

any member funtion if not static, u have to call from object, each object has different address and pointers to member funtions, they may be inline so each mamber funtion may have different address, therefore passing address of funtion with object is not allowed by comilers, however this is not the case of static funtions as the are common to all objects.
hope i m able to make u clear, why ur code will not work.

This article has been dead for over six months. Start a new discussion instead.