#1 Members - Reputation: 261
Posted 26 November 2012 - 07:58 PM
[source lang="cpp"]void order( void (*visit) (int,int) );void visit(int,int);[/source]
They are member function in the same class。
And l want call order like this:
[source lang="cpp"]oder(visit);[/source]
but it can't pass.
How can l do this?
#2 Members - Reputation: 684
Posted 26 November 2012 - 08:07 PM
http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.
#3 Moderators - Reputation: 13594
Posted 26 November 2012 - 08:08 PM
The syntax for pointer-to-member-functions looks like:
class Foo
{
public:
void order( void (Foo::*visit) (int,int) )
{
(this->*visit)(1,2);
}
void visit(int x, int y)
{
printf("%d, %d\n", x, y);
}
void test()
{
order(&Foo::visit);
}
};
#5 Moderators - Reputation: 13594
Posted 27 November 2012 - 02:20 AM
It's a bit confusing because you've called both a variable and a function "visit", so I'll pretend the order definition is instead:
void order( void (Foo::*fn) (int,int) )
"fn" is a pointer, so "*fn" dereferences that pointer as usual. fn was a pointer-to-member-function, so *fn is just a member-function.
i.e. fn is "&Foo::visit" and *fn is "Foo::visit"
"object->" is the regular syntax for accessing members of an object.
Normally you don't have to write "this->" in front of members, but you can if you want to -- e.g. in test you could write "this->order(blah);" and it would mean the same as "order(blah);".
So going off the above, (this-> *fn) ends up meaning this->visit, which is the same as just saying visit.
We need to wrap it in parenthesis (()) and have to include the "this->" because of the weird way that pointer-to-member-function syntax works ;)
So then, (this-> *fn)(1, 2) is the same as visit(1,2), which ends up printing "1 2".
[edit]As wqking corrected below "-> *" shouldn't have a space in it and should be "->*"
Edited by Hodgman, 27 November 2012 - 03:21 AM.
#6 Members - Reputation: 684
Posted 27 November 2012 - 03:02 AM
So going off the above, (this-> *fn) ends up meaning this->visit, which is the same as just saying visit.
I think -> * should be ->* because it's a single operator and we can even overload it.
OP, try to Google for "c++ pointer to member operator", no quote mark.
Here are some search results,
http://msdn.microsoft.com/en-us/library/k8336763%28v=vs.80%29.aspx
http://stackoverflow.com/questions/6586205/what-are-the-pointer-to-member-and-operators-in-c
And Hodgman's reply. :-)
http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.
#7 Members - Reputation: 566
Posted 27 November 2012 - 04:29 AM
#9 Members - Reputation: 1406
Posted 27 November 2012 - 06:00 AM
I highly, highly recommend looking into Boost function (http://www.boost.org/doc/libs/1_52_0/doc/html/function.html). I use it for some callbacks and it works wonders.
Or std::function and/or lambdas on C++11 capable compilers.
#10 Moderators - Reputation: 1325
Posted 29 November 2012 - 11:48 PM
class ITweakable
{
public:
void TweakMe(void) = 0;
}
class Tweakie : ITweakable, IThwackable, //etc...
{
public:
void TweakMe(void)
{
g_Audio->StartPlayback('giggle.ogg');
}
void ThwackMe(void)
{
g_Audio->StartPlayback('oouff.ogg');
}
}
class TweakHerder
{
public:
AddTweakie(ITweakable* tweakie)
{
this->myTweakies.push_back(tweakie);
}
void TimeToTickleTheTweakies(void)
{
//Not C++ because C++ syntax for this is dog-pile
foreach(ITweakable* tweakie in myTweakies)
{
tweakie->TweakMe();
}
}
private:
list<ITweakable> myTweakies;
}
And there ya' go, entertainment for a toddler for hours.
#11 Members - Reputation: 1406
Posted 30 November 2012 - 01:54 AM
There is a reason C++ finally got a proper std::function and lambdas and that is because there are many cases where a full-blown interface just is not the right choice.
Edit: there is also the issue of a true interface always introducing runtime polymorphism (again something that is not an issue with a JITed Java but a potential issue in C++ where runtime polymorphism is often not needed and compile time polymorphism would be enough). But that is just going off on a tangent here...
Edited by BitMaster, 30 November 2012 - 02:16 AM.






