Clarification - Pointer to Function ..

Discussion in 'C++' started by ma740988, Aug 23, 2004.

  1. ma740988

    ma740988 Guest

    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
    ma740988, Aug 23, 2004
    #1
    1. Advertising

  2. 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?
    Alf P. Steinbach, Aug 23, 2004
    #2
    1. Advertising

  3. ma740988

    ma740988 Guest

    (Alf P. Steinbach) wrote in message news:<>...
    > 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.
    ma740988, Aug 23, 2004
    #3
  4. ma740988 wrote in news: 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/
    Rob Williscroft, Aug 23, 2004
    #4
  5. ma740988 wrote:
    >
    > (Alf P. Steinbach) wrote in message news:<>...
    > > 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
    Karl Heinz Buchegger, Aug 23, 2004
    #5
  6. ma740988

    ma740988 Guest

    Rob Williscroft <> wrote in message news:<Xns954E861BC9742ukcoREMOVEfreenetrtw@130.133.1.4>...
    > ma740988 wrote in news: 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.
    ma740988, Aug 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    374
    Victor Bazarov
    Feb 2, 2005
  2. Makwana

    Pointer clarification.

    Makwana, Aug 26, 2008, in forum: C++
    Replies:
    2
    Views:
    307
    peter koch
    Aug 26, 2008
  3. Peter

    pointer/linked List clarification

    Peter, Dec 23, 2008, in forum: C Programming
    Replies:
    5
    Views:
    659
    Peter
    Dec 26, 2008
  4. Tricky

    Pointer clarification needed

    Tricky, Sep 9, 2009, in forum: VHDL
    Replies:
    3
    Views:
    531
    Tricky
    Sep 9, 2009
  5. Ruby Stevenson

    clarification on pointer use

    Ruby Stevenson, Feb 22, 2012, in forum: C++
    Replies:
    4
    Views:
    251
    Jorgen Grahn
    Feb 22, 2012
Loading...

Share This Page