functor object in template class

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

    functor object in template class

    Hi,
    I have a template class that looks like:

    template <class T>
    class Update
    {
    friend otl_stream& operator<< (otl_stream&, const shared_ptr<type name T::row>&);
    friend ostream& operator<< (ostream&, const shared_ptr<type name T::row>&);
    friend struct write_row2datab ase;
    public:
    typedef shared_ptr<type name T::row> ROW;
    typedef typename vector<ROW>::it erator iter;
    typedef typename vector<ROW>::va lue_type val;

    ....

    struct display_row {
    void operator()(val& aVal) {
    cout << aVal;
    }
    };

    struct write_row2datab ase {
    void operator()(val& aVal) {
    // (Update<T>).i << aVal;
    // !!!!! the problem !!!!!
    // i is (private) member of template class, how to address ?
    }

    ....
    bool flush(string& error)
    {
    for_each(v.begi n(), v.end(), display_row()); //works
    for_each(v.begi n(), v.end(), write_row2datab ase());
    }

    private:
    ...
    vector<ROW> v;
    otl_nocommit_st ream i;
    ...

    };

    My question:
    How can I write to i inside write_row2datab ase ,
    what is the correct syntax?
    I declared
    friend struct write_row2datab ase;
    and there exist a operator overloading for "otl_stream<<th eROW".

    Looking wishful forward to a reply,
    Thomas
  • Chandra Shekhar Kumar

    #2
    Re: functor object in template class

    introduce a public member function like getVal for getting the value i then call this
    function in write_row2datab ase

    Comment

    • Rob Williscroft

      #3
      Re: functor object in template class

      porschberg wrote in
      news:8d9566f5.0 306242159.619d6 65e@posting.goo gle.com:
      [color=blue]
      > Hi,
      > I have a template class that looks like:
      >
      > template <class T>
      > class Update
      > {
      > friend otl_stream& operator<<
      > (otl_stream&, const shared_ptr<type name T::row>&);
      > friend ostream& operator<<
      > (ostream&,const shared_ptr<type name T::row>&);[/color]

      // insert this:

      struct write_row2datab ase;
      [color=blue]
      > friend struct write_row2datab ase;[/color]

      /* without the inserted forward declation the
      write_row2datab ase here referes to ::write_row2dat abase
      not Update<T>::writ e_row2database
      */
      [color=blue]
      >public:[/color]
      [snip][color=blue]
      > struct write_row2datab ase {
      > void operator()(val& aVal) {
      > // (Update<T>).i << aVal;
      > // !!!!! the problem !!!!!
      > // i is (private) member of template class, how to address ?
      > }
      >[/color]
      [snip]

      HTH

      Rob.
      --

      Comment

      • porschberg

        #4
        Re: functor object in template class

        Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote in message news:<Xns93A554 3576473ukcoREMO VEfreenetrtw@19 5.129.110.201>. ..[color=blue]
        > porschberg wrote in
        > news:8d9566f5.0 306242159.619d6 65e@posting.goo gle.com:
        >[color=green]
        > > Hi,
        > > I have a template class that looks like:
        > >
        > > template <class T>
        > > class Update
        > > {
        > > friend otl_stream& operator<<
        > > (otl_stream&, const shared_ptr<type name T::row>&);
        > > friend ostream& operator<<
        > > (ostream&,const shared_ptr<type name T::row>&);[/color]
        >
        > // insert this:
        >
        > struct write_row2datab ase;
        >[color=green]
        > > friend struct write_row2datab ase;[/color]
        >
        > /* without the inserted forward declation the
        > write_row2datab ase here referes to ::write_row2dat abase
        > not Update<T>::writ e_row2database
        > */
        >[color=green]
        > >public:[/color]
        > [snip][color=green]
        > > struct write_row2datab ase {
        > > void operator()(val& aVal) {
        > > // (Update<T>).i << aVal;
        > > // !!!!! the problem !!!!!
        > > // i is (private) member of template class, how to address ?
        > > }
        > >[/color]
        > [snip]
        >
        > HTH
        >
        > Rob.[/color]
        Hi Rob, the forward declaration did not help me.
        I think the line (Update<T>).i << aVal; is wrong.
        compiler says:
        Update.hh:101: error: parse error before `;' token
        If I change the line to
        i << aVal;
        I get:
        Update.hh:101: error: 'struct
        Update<P_WHReas onUpdate>::writ e_row2database' has no member named 'i'
        what I understand.
        My aim was to replace the handwritten loop with the for_each
        algorithm but now I see no way to foist this i to the function object.

        Comment

        • Victor Bazarov

          #5
          Re: functor object in template class

          "porschberg " <thomas.porschb erg@osp-dd.de> wrote...[color=blue]
          > Hi,
          > I have a template class that looks like:
          >
          > template <class T>
          > class Update
          > {
          > friend otl_stream& operator<< (otl_stream&, const shared_ptr<type name[/color]
          T::row>&);[color=blue]
          > friend ostream& operator<< (ostream&, const shared_ptr<type name[/color]
          T::row>&);[color=blue]
          > friend struct write_row2datab ase;
          > public:
          > typedef shared_ptr<type name T::row> ROW;
          > typedef typename vector<ROW>::it erator iter;
          > typedef typename vector<ROW>::va lue_type val;
          >
          > ...
          >
          > struct display_row {
          > void operator()(val& aVal) {
          > cout << aVal;
          > }
          > };
          >
          > struct write_row2datab ase {
          > void operator()(val& aVal) {
          > // (Update<T>).i << aVal;
          > // !!!!! the problem !!!!!
          > // i is (private) member of template class, how to address ?
          > }[/color]

          };

          Hereby lies the problem. Class 'write_row2data base', though declared
          nested to "Update" template, knows NOTHING about the Update object
          to which you attempt to output the 'aVal'. What you need to do is
          to create 'write_row2data base' so that it know what 'Update' object
          to use:

          struct write_row2datab ase {
          Update& upd;
          write_row2datab ase(Update& u) : upd(u) {}
          void operator()(val& aVal) {
          upd.i << aVal;
          }
          };
          [color=blue]
          >
          > ...
          > bool flush(string& error)
          > {
          > for_each(v.begi n(), v.end(), display_row()); //works
          > for_each(v.begi n(), v.end(), write_row2datab ase());[/color]

          And here you HAVE to pass the Update object for which the functor
          is created:

          for_each(v.begi n(), v.end(), write_row2datab ase(*this));
          [color=blue]
          > }
          >
          > private:
          > ...
          > vector<ROW> v;
          > otl_nocommit_st ream i;
          > ...
          >
          > };
          >
          > My question:
          > How can I write to i inside write_row2datab ase ,
          > what is the correct syntax?
          > I declared
          > friend struct write_row2datab ase;
          > and there exist a operator overloading for "otl_stream<<th eROW".[/color]

          Victor


          Comment

          • porschberg

            #6
            Re: functor object in template class

            Victor thanks a lot! I understand and it works now.
            thomas
            "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<vfjccbadc a038b@corp.supe rnews.com>...[color=blue]
            > "porschberg " <thomas.porschb erg@osp-dd.de> wrote...[color=green]
            > > Hi,
            > > I have a template class that looks like:
            > >
            > > template <class T>
            > > class Update
            > > {
            > > friend otl_stream& operator<< (otl_stream&, const shared_ptr<type name[/color]
            > T::row>&);[color=green]
            > > friend ostream& operator<< (ostream&, const shared_ptr<type name[/color]
            > T::row>&);[color=green]
            > > friend struct write_row2datab ase;
            > > public:
            > > typedef shared_ptr<type name T::row> ROW;
            > > typedef typename vector<ROW>::it erator iter;
            > > typedef typename vector<ROW>::va lue_type val;
            > >
            > > ...
            > >
            > > struct display_row {
            > > void operator()(val& aVal) {
            > > cout << aVal;
            > > }
            > > };
            > >
            > > struct write_row2datab ase {
            > > void operator()(val& aVal) {
            > > // (Update<T>).i << aVal;
            > > // !!!!! the problem !!!!!
            > > // i is (private) member of template class, how to address ?
            > > }[/color]
            >
            > };
            >
            > Hereby lies the problem. Class 'write_row2data base', though declared
            > nested to "Update" template, knows NOTHING about the Update object
            > to which you attempt to output the 'aVal'. What you need to do is
            > to create 'write_row2data base' so that it know what 'Update' object
            > to use:
            >
            > struct write_row2datab ase {
            > Update& upd;
            > write_row2datab ase(Update& u) : upd(u) {}
            > void operator()(val& aVal) {
            > upd.i << aVal;
            > }
            > };
            >[color=green]
            > >
            > > ...
            > > bool flush(string& error)
            > > {
            > > for_each(v.begi n(), v.end(), display_row()); //works
            > > for_each(v.begi n(), v.end(), write_row2datab ase());[/color]
            >
            > And here you HAVE to pass the Update object for which the functor
            > is created:
            >
            > for_each(v.begi n(), v.end(), write_row2datab ase(*this));
            >[color=green]
            > > }
            > >
            > > private:
            > > ...
            > > vector<ROW> v;
            > > otl_nocommit_st ream i;
            > > ...
            > >
            > > };
            > >
            > > My question:
            > > How can I write to i inside write_row2datab ase ,
            > > what is the correct syntax?
            > > I declared
            > > friend struct write_row2datab ase;
            > > and there exist a operator overloading for "otl_stream<<th eROW".[/color]
            >
            > Victor[/color]

            Comment

            Working...