By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Clarification - Pointer to Function ..

P: n/a
Consider a number of instances of template class
vector<a>, vector<b> ..., vector<f>
The types are - of course different. Of interest is an efficient way
to access them at runtime.

One approach.

if ( condition A is satisfied )
use vector<a>

Presumably some 'relevant pattern' would be ideal compared to a chain
of if's in all related methods.

So in an email I received, I'm told - advisors are great creatures -
to create a heterogeneous container. I've included 'enough' of the
example for traceability.
The example

template<class T>
class Proxy0
{
public:
typedef T TYPE_R;
virtual T CallFunc()=0;
};

//////////////////////////////////////
// Create a Heterogeneous Container
template<typename I, typename T, typename F>
class HetContainer_CommonFunction0 : public I
{
public:
HetContainer_CommonFunction0(T *Src, F f) : TargetClass(Src),
m_f(f){}
I::TYPE_R CallFunc()
{
return ((*TargetClass).*m_f)();
}
~HetContainer_CommonFunction0() { delete TargetClass; }
T *TargetClass;
F m_f;
};

template<typename R, typename T, typename F>
Proxy0<R>* CreateHeterogeneousContainer(T* Src, F f)
{
return new HetContainer_CommonFunction0<Proxy0<R>,T, F>(Src, f);
}
// more

At issue is the call
return ((*TargetClass).*m_f)();

I realize it's a function pointer call, however, the member access
specifier . coupled with the deference operator * for m_f makes the
syntax very confusing to me.

Thanks for your time
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Uh, we don't do homework here.

Although your advisor's ideas and coding style leaves much to be desired.

If you have some more specific problem, do post again with concrete
code (preferably self-describing names also!) illustrating the problem.

* ma740988:
Consider a number of instances of template class
vector<a>, vector<b> ..., vector<f>
The types are - of course different. Of interest is an efficient way
to access them at runtime.

One approach.

if ( condition A is satisfied )
use vector<a>

Presumably some 'relevant pattern' would be ideal compared to a chain
of if's in all related methods.

So in an email I received, I'm told - advisors are great creatures -
to create a heterogeneous container. I've included 'enough' of the
example for traceability.
The example

template<class T>
class Proxy0
{
public:
typedef T TYPE_R;
virtual T CallFunc()=0;
};

//////////////////////////////////////
// Create a Heterogeneous Container
template<typename I, typename T, typename F>
class HetContainer_CommonFunction0 : public I
{
public:
HetContainer_CommonFunction0(T *Src, F f) : TargetClass(Src),
m_f(f){}
I::TYPE_R CallFunc()
{
return ((*TargetClass).*m_f)();
}
~HetContainer_CommonFunction0() { delete TargetClass; }
T *TargetClass;
F m_f;
};

template<typename R, typename T, typename F>
Proxy0<R>* CreateHeterogeneousContainer(T* Src, F f)
{
return new HetContainer_CommonFunction0<Proxy0<R>,T, F>(Src, f);
}
// more

At issue is the call
return ((*TargetClass).*m_f)();

I realize it's a function pointer call, however, the member access
specifier . coupled with the deference operator * for m_f makes the
syntax very confusing to me.

Thanks for your time


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

P: n/a
al***@start.no (Alf P. Steinbach) wrote in message news:<41***************@news.individual.net>...
Uh, we don't do homework here.
How you attribute my request to homework assistance is beyond me.

Although your advisor's ideas and coding style leaves much to be desired.

If you have some more specific problem, do post again with concrete
code (preferably self-describing names also!) illustrating the problem.

It's simple: I was following the source example emailed to me until I
ran across this sytax: "return ((*TargetClass).*m_f)();" I realize
it's a pointer to a function, however, this portion '.*m_f' makes no
sense to me.
Jul 22 '05 #3

P: n/a
ma740988 wrote in news:a5*************************@posting.google.co m in
comp.lang.c++:
At issue is the call
return ((*TargetClass).*m_f)();

I realize it's a function pointer call, however, the member access
specifier . coupled with the deference operator * for m_f makes the
syntax very confusing to me.


There is no "coupling" above only the ".*" operator.

The operator ".*" ( and "->*" ) are for accessing via a member-pointer:

#include <iostream>

struct X
{
int f() { std::cout << "f()\n"; return 0; }
int g() { std::cout << "g()\n"; return 1; }
};

void test( X &x, int (X::*mf)() )
{
int i = (x.*mf)();
std::cout << "test: " << i << '\n';
}

template < typename T, typename F >
void test2( T *x, F f )
{
int i = (x->*f)();
std::cout << "test2: " << i << '\n';
}

int main()
{
X x;

test( x, &X::f );

test2< X, int (X::*)() >( &x, &X::g );

std::cout.flush();
}

Note the explicit template arguments in the call of test2() above
aren't needed here, but you will need them to instantiate a
class template.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

P: n/a
ma740988 wrote:

al***@start.no (Alf P. Steinbach) wrote in message news:<41***************@news.individual.net>...
Uh, we don't do homework here.

How you attribute my request to homework assistance is beyond me.
Although your advisor's ideas and coding style leaves much to be desired.

If you have some more specific problem, do post again with concrete
code (preferably self-describing names also!) illustrating the problem.

It's simple: I was following the source example emailed to me until I
ran across this sytax: "return ((*TargetClass).*m_f)();" I realize
it's a pointer to a function, however, this portion '.*m_f' makes no
sense to me.


..* is a seperate operator in C++.
It is the 'pointer to member' operator.

The intent in your case seems to be to return a pointer to a member function.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

P: n/a
Rob Williscroft <rt*@freenet.co.uk> wrote in message news:<Xn**********************************@130.133 .1.4>...
ma740988 wrote in news:a5*************************@posting.google.co m in
comp.lang.c++:
At issue is the call
return ((*TargetClass).*m_f)();

I realize it's a function pointer call, however, the member access
specifier . coupled with the deference operator * for m_f makes the
syntax very confusing to me.


There is no "coupling" above only the ".*" operator.

The operator ".*" ( and "->*" ) are for accessing via a member-pointer:

#include <iostream>

struct X
{
int f() { std::cout << "f()\n"; return 0; }
int g() { std::cout << "g()\n"; return 1; }
};

void test( X &x, int (X::*mf)() )
{
int i = (x.*mf)();
std::cout << "test: " << i << '\n';
}

template < typename T, typename F >
void test2( T *x, F f )
{
int i = (x->*f)();
std::cout << "test2: " << i << '\n';
}

int main()
{
X x;

test( x, &X::f );

test2< X, int (X::*)() >( &x, &X::g );

std::cout.flush();
}

Note the explicit template arguments in the call of test2() above
aren't needed here, but you will need them to instantiate a
class template.

Excellent. Thanks to both you and Karl.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.