Clarification - Pointer to Function ..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ma740988

    Clarification - Pointer to Function ..

    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<typena me I, typename T, typename F>
    class HetContainer_Co mmonFunction0 : public I
    {
    public:
    HetContainer_Co mmonFunction0(T *Src, F f) : TargetClass(Src ),
    m_f(f){}
    I::TYPE_R CallFunc()
    {
    return ((*TargetClass) .*m_f)();
    }
    ~HetContainer_C ommonFunction0( ) { delete TargetClass; }
    T *TargetClass;
    F m_f;
    };

    template<typena me R, typename T, typename F>
    Proxy0<R>* CreateHeterogen eousContainer(T * Src, F f)
    {
    return new HetContainer_Co mmonFunction0<P roxy0<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
  • Alf P. Steinbach

    #2
    Re: Clarification - Pointer to Function ..

    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:[color=blue]
    > 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<typena me I, typename T, typename F>
    > class HetContainer_Co mmonFunction0 : public I
    > {
    > public:
    > HetContainer_Co mmonFunction0(T *Src, F f) : TargetClass(Src ),
    > m_f(f){}
    > I::TYPE_R CallFunc()
    > {
    > return ((*TargetClass) .*m_f)();
    > }
    > ~HetContainer_C ommonFunction0( ) { delete TargetClass; }
    > T *TargetClass;
    > F m_f;
    > };
    >
    > template<typena me R, typename T, typename F>
    > Proxy0<R>* CreateHeterogen eousContainer(T * Src, F f)
    > {
    > return new HetContainer_Co mmonFunction0<P roxy0<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[/color]

    --
    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?

    Comment

    • ma740988

      #3
      Re: Clarification - Pointer to Function ..

      alfps@start.no (Alf P. Steinbach) wrote in message news:<4129656b. 27561937@news.i ndividual.net>. ..[color=blue]
      > Uh, we don't do homework here.
      >[/color]
      How you attribute my request to homework assistance is beyond me.

      [color=blue]
      > 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.
      >[/color]
      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.

      Comment

      • Rob Williscroft

        #4
        Re: Clarification - Pointer to Function ..

        ma740988 wrote in news:a5ae824.04 08221918.420033 68@posting.goog le.com in
        comp.lang.c++:
        [color=blue]
        > 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.
        >
        >[/color]

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

        Comment

        • Karl Heinz Buchegger

          #5
          Re: Clarification - Pointer to Function ..

          ma740988 wrote:[color=blue]
          >
          > alfps@start.no (Alf P. Steinbach) wrote in message news:<4129656b. 27561937@news.i ndividual.net>. ..[color=green]
          > > Uh, we don't do homework here.
          > >[/color]
          > How you attribute my request to homework assistance is beyond me.
          >[color=green]
          > > 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.
          > >[/color]
          > 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.[/color]

          ..* 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
          kbuchegg@gascad .at

          Comment

          • ma740988

            #6
            Re: Clarification - Pointer to Function ..

            Rob Williscroft <rtw@freenet.co .uk> wrote in message news:<Xns954E86 1BC9742ukcoREMO VEfreenetrtw@13 0.133.1.4>...[color=blue]
            > ma740988 wrote in news:a5ae824.04 08221918.420033 68@posting.goog le.com in
            > comp.lang.c++:
            >[color=green]
            > > 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.
            > >
            > >[/color]
            >
            > 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.[/color]


            Excellent. Thanks to both you and Karl.

            Comment

            Working...