Jump to content

  • Log In with Google      Sign In   
  • Create Account


A problem about a pointer to member function?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 bluepig.man   Members   -  Reputation: 411

Like
0Likes
Like

Posted 26 November 2012 - 07:58 PM

L have declare two function like this:

[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?

Sponsor:

#2 wqking   Members   -  Reputation: 756

Like
2Likes
Like

Posted 26 November 2012 - 08:07 PM

void (MyClass::*visit) (int,int)

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 Hodgman   Moderators   -  Reputation: 29712

Like
3Likes
Like

Posted 26 November 2012 - 08:08 PM

You're using a regular pointer-to-function, not a pointer-to-member-function.
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);
	}
};


#4 bluepig.man   Members   -  Reputation: 411

Like
0Likes
Like

Posted 27 November 2012 - 01:53 AM

		(this->*visit)(1,2);

thank s for your answer,but what's the meaning of this?

#5 Hodgman   Moderators   -  Reputation: 29712

Like
3Likes
Like

Posted 27 November 2012 - 02:20 AM

That's the syntax for calling a pointer-to-member-function.

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 wqking   Members   -  Reputation: 756

Like
4Likes
Like

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 KaiserJohan   Members   -  Reputation: 1126

Like
1Likes
Like

Posted 27 November 2012 - 04:29 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.

#8 bluepig.man   Members   -  Reputation: 411

Like
0Likes
Like

Posted 27 November 2012 - 04:40 AM

l got the point.Thanks for the answer,

Edited by bluepig.man, 27 November 2012 - 04:40 AM.


#9 BitMaster   Crossbones+   -  Reputation: 3918

Like
1Likes
Like

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 Shannon Barber   Moderators   -  Reputation: 1362

Like
0Likes
Like

Posted 29 November 2012 - 11:48 PM

You are probably much better off learning about 'Interfaces' to solve this problem if you are going to use a pointer-to-a-member-function to solve it.

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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

#11 BitMaster   Crossbones+   -  Reputation: 3918

Like
3Likes
Like

Posted 30 November 2012 - 01:54 AM

Going whole interfaces for a single functor is a bit of overkill and often the sign of Javaisms creeping into C++ (not trying to whack on Java here, but the two languages require a very different mindset).

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.


#12 bluepig.man   Members   -  Reputation: 411

Like
0Likes
Like

Posted 10 December 2012 - 05:30 AM

Posted Imagethanks for your answer,have a good time.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS