C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 07-16-2009, 02:11 PM   #16
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> That's why there are two overloads! One for const, one for non-const.

What, you expect me to actually read the code?

>> I'm just concerned about returning void from the FncPtr call.

Code:
template < typename R >
R foo( R ( *f )( ) )
{
	return f( );
}

int bar( void )
{
	return 3114;
}

void qux( void )
{	}

int main( void )
{
	cout << foo( bar ) << endl;
	foo( qux );
	return 0;
}
Sebastiani is offline   Reply With Quote
Old 07-16-2009, 02:26 PM   #17
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by Sebastiani View Post
>> That's why there are two overloads! One for const, one for non-const.
What, you expect me to actually read the code?
Aww, come on. It's not that big and complicated.
So this actually compiles, where you try to return "void"?
__________________
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, 02:32 PM   #18
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> Aww, come on. It's not that big and complicated.

Oh I know, I should have paid closer attention.

>> So this actually compiles, where you try to return "void"?

Yep. As long as you don't try to explicitly declare a variable of that type, though (which is really too bad because that would have been a useful feature). So in other words, this would not compile if used with void:

Code:
template < typename R >
R foo( R ( *f )( ) )
{
	R r = f( );		
	return r;
}
Sebastiani is offline   Reply With Quote
Old 07-16-2009, 02:34 PM   #19
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Alright, cool, no worries about that then.
__________________
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, 02:40 PM   #20
Registered User
 
Join Date: Jul 2009
Posts: 11
Quote:
Originally Posted by Elysia View Post
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
I cant read this because its looked...

And, I cant see but when I am compiling that, its returning a error:
Code:
 In function ‘void example2(Class_t&, Fnc_t) [with Class_t = ExClass, Fnc_t = int (ExClass::*)(const int&)]’:
16:   instantiated from here
10: error: too few arguments to function
What is wrong?
tharnier is offline   Reply With Quote
Old 07-16-2009, 02:49 PM   #21
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Ah, that's because the function takes an int, but we don't pass one. You can fix it like:
(Class.*FncPtr)(10);
func(10);
__________________
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, 02:53 PM   #22
Registered User
 
Join Date: Jul 2009
Posts: 11
Quote:
Originally Posted by Elysia View Post
Ah, that's because the function takes an int, but we don't pass one. You can fix it like:
(Class.*FncPtr)(10);
func(10);
hmmmm that is what I was trying to avoid...
But I'm trying to use boost::function...

Do you know some really good book to indicate to me??? with this kind of doubt
tharnier is offline   Reply With Quote
Old 07-16-2009, 02:58 PM   #23
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Oh? Then let's borrow the power of boost::bind a little more:
example( boost::bind(&ExClass::func, f, 10) );
Notice that I put a number in there as well.
Now
func();
Simply will work.
Praised be boost!
__________________
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, 03:08 PM   #24
Registered User
 
Join Date: Jul 2009
Posts: 11
What probably will solve my problem is:
Code:
ExClass f;
f.func = boost::bind( &ExClass::func, &f );
example( f.func );
isn't?

But, do I really need to install all boost library to use boost::bind()???
tharnier is offline   Reply With Quote
Old 07-16-2009, 03:11 PM   #25
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
It doesn't work that way! Just pass what boost::bind returns directly to your function.
You need to download the boost package, yes, but you don't need to install any dlls to use it. However, you can't just break it into pieces since it has dependencies on other headers in the boost distribution.

Code:
ExClass f;
example( boost::bind(&ExClass::func, f, 1000) );
__________________
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, 03:14 PM   #26
Registered User
 
Join Date: Jul 2009
Posts: 11
Quote:
Originally Posted by Elysia View Post
It doesn't work that way! Just pass what boost::bind returns directly to your function.
You need to download the boost package, yes, but you don't need to install any dlls to use it. However, you can't just break it into pieces since it has dependencies on other headers in the boost distribution.

Code:
ExClass f;
example( boost::bind(&ExClass::func, f, 1000) );
Ok, and how about the book???
Can you suggest one??
To look up that kind of doubt?
tharnier is offline   Reply With Quote
Old 07-16-2009, 03:17 PM   #27
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Not much about boost, sadly.
There is one book about boost, I think. Can't remember its name. It's somewhat old and doesn't cover that much, but it's about the only one available or so. A search on the web should reveal more information about that.
Otherwise the boost homepage is your best source of information.
__________________
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, 03:20 PM   #28
Registered User
 
Join Date: Jul 2009
Posts: 11
Quote:
Originally Posted by Elysia View Post
Not much about boost, sadly.
There is one book about boost, I think. Can't remember its name. It's somewhat old and doesn't cover that much, but it's about the only one available or so. A search on the web should reveal more information about that.
Otherwise the boost homepage is your best source of information.
No no, not about boost... about c++, but not too beginer, I say intermediate!!!!
tharnier is offline   Reply With Quote
Old 07-16-2009, 03:23 PM   #29
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Oh, okay. Then check the books thread.
If you haven't checked out Accelerated C++, then you should probably do that too.
It doesn't cover just the basics; it covers a lot of more and a lot of it is handy stuff!
__________________
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, 03:25 PM   #30
Registered User
 
Join Date: Jul 2009
Posts: 11
Quote:
Originally Posted by Elysia View Post
Oh, okay. Then check the books thread.
If you haven't checked out Accelerated C++, then you should probably do that too.
It doesn't cover just the basics; it covers a lot of more and a lot of it is handy stuff!
Ok, thank you all your attention and patient to my problem!!!!
tharnier 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