pointer to a member function?

A

aaragon

Hi everybody, I was wondering if there is an easy solution to this
problem in order to avoid writing some extra code:

I have a template class that has a member function called map:

template <class P>
class Q {

typedef P::R RType;
...

RType* map(P*, P*, int) {
... // do some stuff
}
};

Now, in another function contained in class Q, I need to sort a vector
and for that I use a functor:

template <class P>
class SortFunctor {
typedef typename P::R RType;
P* p1_;
P* p2_;
public:
SortFunctor(P* p1, P* p2)
: p1_(p1), p2_(p2) {}
bool operator()(size_t left, size_t right) {
return (map(left) < map(right));
}
RType* map(index) {
if (index < p1_->size())
return p1_->rtype(index);
else
return p2_->rtype(index-p1_->size());
}
};

So, this works perfectly, but I write the same function map in both
the functor and in the template class Q. Now, I thought that it might
be cool to use a function to a pointer, so I added some extra stuff to
the functor so it looks like this (and of course I removed the
function map()):

template <class P>
class SortFunctor {
typedef typename P::R RType;
P* p1_;
P* p2_;
Rtype* (*map)(P*,P*,int);
public:
SortFunctor(P* p1, P* p2, RType* fp)
: p1_(p1), p2_(p2) { map = fp; }
....
};

and I have the following error message:

error: ISO C++ forbids taking the address of an unqualified or
parenthesized non-static member function to form a pointer to member
function.

Does anyone know how to do this? I appreciate any suggestions.

 
B

Barry

aaragon said:
Hi everybody, I was wondering if there is an easy solution to this
problem in order to avoid writing some extra code:

I have a template class that has a member function called map:

DON'T have a name that is already used in standard library as your
identifier. I hardly can go on with your code.
template <class P>
class Q {

typedef P::R RType;

dependent type,

typedef typename P::R RType;
...

RType* map(P*, P*, int) {
... // do some stuff
}
};

Now, in another function contained in class Q, I need to sort a vector
and for that I use a functor:

template <class P>
class SortFunctor {
typedef typename P::R RType;
P* p1_;
P* p2_;
public:
SortFunctor(P* p1, P* p2)
: p1_(p1), p2_(p2) {}
bool operator()(size_t left, size_t right) {
return (map(left) < map(right));
}
RType* map(index) {

RType* map(size_t index) {
if (index < p1_->size())
return p1_->rtype(index);
else
return p2_->rtype(index-p1_->size());
}
};

So, this works perfectly, but I write the same function map in both

Already works? I think it does not even compile
the functor and in the template class Q. Now, I thought that it might
be cool to use a function to a pointer, so I added some extra stuff to
the functor so it looks like this (and of course I removed the
function map()):

template <class P>
class SortFunctor {
typedef typename P::R RType;
P* p1_;
P* p2_;
Rtype* (*map)(P*,P*,int);
public:
SortFunctor(P* p1, P* p2, RType* fp)
: p1_(p1), p2_(p2) { map = fp; }
...
};

and I have the following error message:

error: ISO C++ forbids taking the address of an unqualified or
parenthesized non-static member function to form a pointer to member
function.

On the above, map is pointer to function, which is different from
pointer to member function.

I guess you write something like: &Klass::Fun, this is a pointer to
member function, its type is: ResultType (KlassType::*)(Arg1Type, Arg2Type);

If you have problem in pointer to member function, try to check it out
first.
 
A

aaragon

DON'T have a name that is already used in standard library as your
identifier. I hardly can go on with your code.





dependent type,

typedef typename P::R RType;







RType* map(size_t index) {



Already works? I think it does not even compile







On the above, map is pointer to function, which is different from
pointer to member function.

I guess you write something like: &Klass::Fun, this is a pointer to
member function, its type is: ResultType (KlassType::*)(Arg1Type, Arg2Type);

If you have problem in pointer to member function, try to check it out
first.

Well, the code I wrote is not the actual code that I used, I
simplified it a lot to remove things that were not meaningful to the
problem. The function is not called map but decodeIndividual (but it
is basically a mapping, therefore the name that I chose for this
post). But still, what I'm asking is if what I'm trying to accomplish
is feasible or not. I read on the FAQ Lite and I found out that it is
very tricky to use pointers to member functions. They give a solution
giving a type definition, but of course that doesn't apply to my
problem because I deal with template classes and template functors.
 
B

Barry

aaragon said:
Well, the code I wrote is not the actual code that I used, I
simplified it a lot to remove things that were not meaningful to the
problem. The function is not called map but decodeIndividual (but it
is basically a mapping, therefore the name that I chose for this
post). But still, what I'm asking is if what I'm trying to accomplish
is feasible or not. I read on the FAQ Lite and I found out that it is
very tricky to use pointers to member functions. They give a solution
giving a type definition, but of course that doesn't apply to my
problem because I deal with template classes and template functors.

View the code /binder1st/ /bind1st/ and /binder2nd/ /bind2nd/ in STL,
then you will know how to trait out the type of a pointer to member
function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top