Clarification - Pointer to Function ..

M

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

Alf P. Steinbach

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:
 
M

ma740988

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.
 
R

Rob Williscroft

ma740988 wrote in 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.
 
K

Karl Heinz Buchegger

ma740988 said:
How you attribute my request to homework assistance is beyond me.

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.
 
M

ma740988

Rob Williscroft said:
ma740988 wrote in in
comp.lang.c++:


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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top