Is it possible, like functions, to pass methods as arguments for other methods? If so, what is the syntax?
Printable View
Is it possible, like functions, to pass methods as arguments for other methods? If so, what is the syntax?
I wrote something like this:
But don't know how to call function on which pointer p points. If I remove comments I get compiler error wich means that I'm doing something very, very wrong. Must admit I never understood concept of pointers to member functions completely.Code:#include <iostream>
using namespace std;
class Test{
int x;
typedef void (Test::*ptr) ( );
public:
void show ( )
{
cout << x << endl;
}
Test ( int i ): x(i)
{
}
void other_member(ptr p)
{
// p();//this is compiler error
}
};
int main ( )
{
Test t(3);
t.other_member( &Test::show );
return 0;
}
Need to read that article!
Could try:Quote:
But don't know how to call function on which pointer p points.
Code:void Test::other_member(ptr p)
{
(this->*p)();
}
I never thought about that, thanks laserlight!
I tried (*p)(); and p(); but it didn't occure to me to try with "this" pointer.
I'm not sure I understand meaning of ->*. Can this part:
be interpreted as: for this object, find on which function points p and then execute it?Code:this->*p
please explain a little more real meaning of ->*?
The arrow operator (->) is a language simplification of a common pointer usage. This usage is "dereference followed by the dot operator".
Imagine a class called Car with a function member called Break. Imagine an object of this class called myCar. We normally access the function member with myCar.Break().
But what to do if we then create a pointer to the myCar object?
Car* myBrokenCar = &myCar;
In order to access the Break member of myBrokenCar, it seems at first the sensible thing is to dereference the pointer and then access the function member:
*myBrokenCar.Break();
But that is an error. The problem is the dot operator has an higher precedence than the dereference operator. Has such, the compiler first tries to call the Break() function from myBrokenCar and only then to dereference the result. But since myBrokenCar is a pointer, it has no members. The above code gives a compiler error.
The solution is then to force the dereference operator to act first by parenthesing it.
(*myBrokenCar).Break();
And that's it. Now it will work fine. But this usage is so common in C++ that the arrow operator was created to simplify the syntax.
myBrokenCar->Break();
Means dereference my left argument and apply the dot operator with my right argument. Just the same as the previous code.
As such, I think it is easy for you to now see what it means to write ->*.
If Break was itself a pointer to a function, it would mean to also derefer Break.
Thank you Mario f. for explanation, but operator -> and it's meaning is known to me. However, I have trouble undertanding :
why (this->*p)() works and why (this->(*p))() is an error?
So, I need explanation regarding this particular problem, not general exaplanations about pointers and the way they're used.
Ah. Didn't quite get your question before.
Well, that happens with and without a pointer. And it happens for the same reason you can't do this...
myCar.(Break())
What is happening here?
Break() is being evaluated even before you give the compiler a chance to make it a member of myCar.
With pointers...
(*myCar).(Break())
The same happens.
And with the arrow operator plus a function member pointer, the same thing happens...
myCar->(*Break)
And your particular example is even more insidious ;) Being a function argument and all
Ok, I understand now!
Thanks
By the language definition, ".*" and "->*" are single operators, i.e. if you put parentheses before the *, it suddenly becomes two operators. And you cannot apply * to member pointers, only .* and ->*.
FYI there are much cleaner ways of doing this using boost::bind, boost::function or even std::mem_fun.
Thank you a lot guys!!!
Quote:
Originally Posted by ChaosEngine
Nice. Now... if only I could install boost on my system...
You don't need to install it to use most of the boost libraries. Many are just simple headers that you can include in your program and use as is.
why can't you install boost? PM me if you have trouble with it, and I'll be happy to give you a hand.Quote:
Originally Posted by Mario F.