Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing member function pointers are template parameters

Reply
Thread Tools

Passing member function pointers are template parameters

 
 
Dilip
Guest
Posts: n/a
 
      11-06-2006

I am stuck with a syntactical issue and would appreciate some help:

I have a class with a templated mem fn like so:

class A
{
};

template<typename Ret, typename P1, Ret (A::*fp)(P1*)>
void A::somefunc()
{
P1* p = getP1FromSomewhere();
someotherfunc(p, fp);
}

// should I re-write the whole template shebang for someotherfunc?
// I tried (probably w/o understanding completely)
template<typename X, typename T>
void A::someotherfunc(X* p1, T func)
{
// fails with compiler error the statement does not evaluate to a
function being called
// with one parameter
func(p1);
}

What is the correct way to pass function pointers in a cascade?

P.s: I am mucking around with legacy code and although there might be
better ways of acheiving what I want, I am not in a position to make
whole-sale code changes

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-06-2006
Dilip wrote:
> I am stuck with a syntactical issue and would appreciate some help:
>
> I have a class with a templated mem fn like so:
>
> class A
> {
> };
>
> template<typename Ret, typename P1, Ret (A::*fp)(P1*)>
> void A::somefunc()
> {
> P1* p = getP1FromSomewhere();
> someotherfunc(p, fp);
> }
>
> // should I re-write the whole template shebang for someotherfunc?
> // I tried (probably w/o understanding completely)
> template<typename X, typename T>
> void A::someotherfunc(X* p1, T func)
> {
> // fails with compiler error the statement does not evaluate to a
> function being called
> // with one parameter
> func(p1);
> }
>
> What is the correct way to pass function pointers in a cascade?


Yes, but it's not a correct way to use a pointer to member. You need
an instance of the class. Did you mean to write

(this->*func)(p1);

(or some such)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      11-06-2006

Dilip wrote:
> I am stuck with a syntactical issue and would appreciate some help:
>
> I have a class with a templated mem fn like so:
>
> class A
> {
> };
>
> template<typename Ret, typename P1, Ret (A::*fp)(P1*)>
> void A::somefunc()
> {
> P1* p = getP1FromSomewhere();
> someotherfunc(p, fp);
> }
>
> // should I re-write the whole template shebang for someotherfunc?
> // I tried (probably w/o understanding completely)
> template<typename X, typename T>
> void A::someotherfunc(X* p1, T func)
> {
> // fails with compiler error the statement does not evaluate to a
> function being called
> // with one parameter
> func(p1);
> }
>
> What is the correct way to pass function pointers in a cascade?
>
> P.s: I am mucking around with legacy code and although there might be
> better ways of acheiving what I want, I am not in a position to make
> whole-sale code changes


Not 100% sure about your class requirements as the defintion is
empty...but here is what I could come up with based on the function
sgnatures you had..

class P1
{
};

class P
{
public:
void Pfunc(P1* p1_ptr)
{
};
};

class A
{
public:
template<typename Ret, typename tP1, typename tP2, Ret
(tP1::*fp)(tP2*)>
void somefunc(tP1* p) ;

template<typename X, typename T>
void someotherfunc(X* p1, T func);
};


template<typename Ret, typename tP1, typename tP2, Ret
(tP1::*fp)(tP2*)>
void A::somefunc(tP1* p)
{
someotherfunc(p, fp);
}


template<typename X, typename T>
void A::someotherfunc(X* p, T func)
{
P1* p1new = new P1;
(p->*func)(p1new);
delete p1new;
}

 
Reply With Quote
 
Dilip
Guest
Posts: n/a
 
      11-06-2006
Victor Bazarov wrote:
> > What is the correct way to pass function pointers in a cascade?

>
> Yes, but it's not a correct way to use a pointer to member. You need
> an instance of the class. Did you mean to write
>
> (this->*func)(p1);


Thanks! Thats exactly what I was looking for.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
passing member function pointers to a function tbringley@gmail.com C++ 18 02-26-2007 05:46 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments