C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-16-2009, 12:19 PM   #1
Registered User
 
Join Date: Jul 2009
Posts: 11
Problem: passing function as argument !!!

Hi, I'm having a problem in pass a function as argument to another function.
This call is made by reference, and the function called is declared into a class.
A example of my problem is:

Code:
class ExClass {
       public:
             int func(const int &); //***
};

int ExClass::func(const int &z) { return(z); }

template <class T>
void example(T &func) {}

int main(){
     ExClass f;
     example( f.func );
     return(0);
} 
When I compile that, I got such error:

In function ‘int main()’:
error: no matching function for call to ‘example(<unresolved overloaded function type>)’
note: candidates are: void example(T&) [with T = int (ExClass::*)(const int&)]



*** It's good to mention that if I declared "func" as "static int func(const int &);" into the "ExClass", I get no error. But I need let "func" be non-static.

What am I supposed to do?
Thank you.
tharnier is offline   Reply With Quote
Old 07-16-2009, 12:23 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
You can't get from a "plain" function pointer to a pointer to member function, or back the other way. (Since a member function has to be called from an object of the class, and has to know about that object, etc., which a "plain" function pointer can't.)

What are you trying to do, really?
tabstop is offline   Reply With Quote
Old 07-16-2009, 12:24 PM   #3
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> Hi, I'm having a problem in pass a function as argument to another function.

It can't work because you're passing in a function that takes an implicit pointer to *this, which you haven't supplied, and without an object the function cannot be called, obviously.
Sebastiani is offline   Reply With Quote
Old 07-16-2009, 12:37 PM   #4
Registered User
 
Join Date: Jul 2009
Posts: 11
I'm intending to use a 'ready' function, Broydn, from 'Numerical Recipes Book". And that function, Broydn, calls aother function by reference. For example:
Code:
template <class T>
void Broydn( T &func ) {}
When my member function, "func", was declared as 'static', so I could use the syntax:
Code:
int main(){
     ExClass f;
     Broydn( f.func );
     return(0);
}
But, I can't understand why, if I declare my member function as non-static it doesn't work.

Thank you.
tharnier is offline   Reply With Quote
Old 07-16-2009, 12:40 PM   #5
Registered User
 
Join Date: Oct 2006
Posts: 263
Quote:
Originally Posted by tharnier View Post
...if I declared "func" as "static int func(const int &);" into the "ExClass", I get no error. But I need let "func" be non-static.
I don't think you can do this without a static member. I tried a few different things on my own, and it seems you can't take the address of a non-static member function.
Elkvis is offline   Reply With Quote
Old 07-16-2009, 12:44 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Well, I had thought we had covered why: You would need an ExClass object to call your function with -- and Broydn quite simply won't have such an object, and therefore quite simply won't be able to call the function. What is this object ExClass doing for you? (I mean, I had to look up Broyden's method just now, and it appears to be a root-finder.) If you need this class, I suppose you can adjust your root-finder to take an object and a member function pointer, so that your root finder will have the object needed to call the function with.
tabstop is offline   Reply With Quote
Old 07-16-2009, 12:49 PM   #7
Registered User
 
Join Date: Jul 2009
Posts: 11
Quote:
Originally Posted by Elkvis View Post
I don't think you can do this without a static member. I tried a few different things on my own, and it seems you can't take the address of a non-static member function.
hmmmm, sorry if I was not clear. I think this explain why I couldn't do:

Code:
int main(){
    ExClass f, *p=&f;
    p -> func;
...
}
because I cant take the address of a non-static member function, am I right?
tharnier is offline   Reply With Quote
Old 07-16-2009, 12:55 PM   #8
Registered User
 
Join Date: Jul 2009
Posts: 11
Quote:
Originally Posted by tabstop View Post
Well, I had thought we had covered why: You would need an ExClass object to call your function with -- and Broydn quite simply won't have such an object, and therefore quite simply won't be able to call the function. What is this object ExClass doing for you? (I mean, I had to look up Broyden's method just now, and it appears to be a root-finder.) If you need this class, I suppose you can adjust your root-finder to take an object and a member function pointer, so that your root finder will have the object needed to call the function with.
ow yeah, I think you really helped me right here!!!
Broydn is a root finder, you're right. And you let my problem clear; I need that Broydn be able to get an object from my class ExClass.
But how should I do that?

I thought that if I had declared Broydn as "friend" of "ExClass", so Broydn could use a member function even without a object, couldn't??

Last edited by tharnier; 07-16-2009 at 12:59 PM.
tharnier is offline   Reply With Quote
Old 07-16-2009, 01:03 PM   #9
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Two examples.
One using boost, the other without boost.
Code:
class ExClass 
{
public:
    int func(const int& z); //***
};

int ExClass::func(const int& z) { return z; }

template <typename T>
void example(T& func) { func(); }

template <typename Class_t, typename Fnc_t>
void example2(Class_t& Class, Fnc_t FncPtr) { (Class.*FncPtr)(); }

int main()
{
     ExClass f;
     example( boost::bind(&ExClass::func, f) );
     example2(f, &ExClass::func);
     return 0;
}
And don't forget to read this:
https://apps.sourceforge.net/mediawi...arameter_names

Quote:
Originally Posted by tharnier View Post
I thought that if I had declared Broydn as "friend" of "ExClass", so Broydn could use a member function even without a object, couldn't??
Of course not. That's just silly. Friends has nothing to do with it.
The problem is that the functions are members OF objects. They are not global!
So if you want to call a member function, you need to specify which class instance's member function you want to call!
Thus you need the instance AND the member function pointer.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-16-2009, 01:04 PM   #10
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
What you're looking for is a 'functor'. In its crudest form it would work something like this:

Code:
class ExClass {
	public:
        
	int func(const int & value)
	{
		cout << "Value: " << value << endl;
		return value;
	}

        struct wrapper
	{
		wrapper(ExClass* this_)
		: this_(this_)
		{	}
	
		int operator ( )(const int & value)
		{
			return this_->func(value);
		}
		
		ExClass* this_;
	};
	
	wrapper wrap(void)
	{
		return wrapper(this);
	}	
};

template <class T>
void example(T &func) 
{
	int value = 10;
	func(value); 
}

int main(void) {
	ExClass
		e;
	ExClass::wrapper
		f = e.wrap( );
	example(f);		
}

Last edited by Sebastiani; 07-16-2009 at 01:51 PM. Reason: forgotten return values
Sebastiani is offline   Reply With Quote
Old 07-16-2009, 01:13 PM   #11
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
In that case, I'll throw in another, better example:
Code:
class ExClass 
{
public:
    int func(const int& z); //***
};

int ExClass::func(const int& z) { return z; }

template<typename Class_t, typename Fnc_t, typename Return_t>
class Functor
{
public:
    Functor(Class_t& Class, Fnc_t FncPtr): m_Class(Class), m_FncPtr(FncPtr) { }
    Return_t operator () { return m_FncPtr(); }
    const Return_t operator () const { return m_FncPtr(); }

private:
    Class_t m_Class;
    Fnc_t m_FncPtr;
};

template<typename Return_t, typename Class_t, typename Fnc_t>
Functor<Class_t, Fnc_t, Return_t> FunctorWrapper(Class_t& Class, Fnc_t FncPtr)
{
    return Functor<Class_t, Fnc_t, Return_t>(Class, FncPtr);
}

template<typename T> void example(T& Functor) { Functor(); }

int main()
{
     ExClass f;
     example( FunctorWrapper<int>(f, &ExClass::func) );
     return 0;
}
I'm not sure how it handles void return type, however. It may be that it needs extra work. Perhaps a specialization.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-16-2009, 01:24 PM   #12
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> I'm not sure how it handles void return type, however.

AFAIK, it should handle that automagically. I could be wrong, though.

>> const Return_t

You probably shouldn't return const like that. The user might not want it to be, and anyway it wouldn't work if the type was already const. At any rate, yours is a much better way to handle a functor...I was just being lazy.
Sebastiani is offline   Reply With Quote
Old 07-16-2009, 01:30 PM   #13
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by tharnier View Post
I thought that if I had declared Broydn as "friend" of "ExClass", so Broydn could use a member function even without a object, couldn't??
"friend" just means that it can access private variables of an object which it still needs to have -- you can't still talk about "the" variable x, it's bob's variable x, or fred's variable x, or joe's variable x, et cetera. Members don't exist by themselves, only as part of other objects (hence the name).
tabstop is offline   Reply With Quote
Old 07-16-2009, 01:43 PM   #14
Registered User
 
Join Date: Jul 2009
Posts: 11
Quote:
Originally Posted by Elysia View Post
In that case, I'll throw in another, better example:
I think that will work. But I'm just taking my time to understand the code....
Thank you everyone.
tharnier is offline   Reply With Quote
Old 07-16-2009, 01:54 PM   #15
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by Sebastiani View Post
>> I'm not sure how it handles void return type, however.

AFAIK, it should handle that automagically. I could be wrong, though.
I'm just concerned about returning void from the FncPtr call.

Quote:
>> const Return_t

You probably shouldn't return const like that. The user might not want it to be, and anyway it wouldn't work if the type was already const. At any rate, yours is a much better way to handle a functor...I was just being lazy.
That's why there are two overloads! One for const, one for non-const.
It's critical if we pass the object through a const reference.
So don't use "const" in the type and it'll work
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
argument, function, overloaded, reference, type

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing A Function Into A Constructor fourplay C++ Programming 6 03-15-2009 06:06 AM
Compiling sample DarkGDK Program Phyxashun Game Programming 6 01-27-2009 03:07 AM
Passing function as argument NeMewSys C Programming 16 06-11-2008 02:03 PM
passing a function as an argument angelscars C++ Programming 6 12-06-2005 09:53 PM
Didn't quite know where to post this.......compiler problem... incognito C++ Programming 5 02-08-2003 07:42 PM


All times are GMT -6. The time now is 06:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22