RWList pass by reference -probkem

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

    RWList pass by reference -probkem

    Hello,

    I'm passing a RWList object of one complex data to function f(). In
    the function f() i'm receiving in that list as reference. In the
    function f(), i want to modify one member variable in all the nodes in
    that input RWList. I've written following (dummy)code to do produce my
    problem. The values modified in the function are not reflected in the
    called function. I spend sometime not able to find/resolve the
    problem...

    class A{
    public:
    const RWString & x( return _x;}
    const RWstring & y( return _y;}
    void x(const RWStrng & value) { _x = value; }
    void y(const RWStrng & value) { _y = value; }

    private:
    RWString _x;
    RWString _y;
    };

    void f ( RWList<A>& alist) // receiving as reference of that list
    {
    RWListIter<A> AIter(alist);

    while (AIter())
    {
    A &a = AIter.key(); // storing as reference
    a.x("xxxxxxxxx" );
    }

    }
    Main ()
    {
    RWList<A> alist;
    A a1,a2;
    ...
    a1.y("something ");
    a2.y("asdasdasa ");
    ...
    alist.insert(a1 );
    alist.inset(a2) ;
    ...
    f(alist);
    ...
    }


    Please correct if anything wrong in my code.

    It appears that begin and end function are not provided in the RWList
    class. ( I tried AList.begin() and AList.end() )

    Thanks,
    Sangeetha.
  • Karl Heinz Buchegger

    #2
    Re: RWList pass by reference -probkem

    sangeetha wrote:[color=blue]
    >
    > Hello,
    >
    > I'm passing a RWList object of one complex data to function f(). In
    > the function f() i'm receiving in that list as reference. In the
    > function f(), i want to modify one member variable in all the nodes in
    > that input RWList. I've written following (dummy)code to do produce my
    > problem. The values modified in the function are not reflected in the
    > called function. I spend sometime not able to find/resolve the
    > problem...
    >
    > class A{
    > public:
    > const RWString & x( return _x;}
    > const RWstring & y( return _y;}
    > void x(const RWStrng & value) { _x = value; }
    > void y(const RWStrng & value) { _y = value; }
    >
    > private:
    > RWString _x;
    > RWString _y;
    > };
    >
    > void f ( RWList<A>& alist) // receiving as reference of that list
    > {
    > RWListIter<A> AIter(alist);
    >
    > while (AIter())
    > {
    > A &a = AIter.key(); // storing as reference
    > a.x("xxxxxxxxx" );
    > }
    >
    > }
    > Main ()
    > {
    > RWList<A> alist;
    > A a1,a2;
    > ...
    > a1.y("something ");
    > a2.y("asdasdasa ");
    > ...
    > alist.insert(a1 );
    > alist.inset(a2) ;
    > ...
    > f(alist);
    > ...
    > }
    >
    > Please correct if anything wrong in my code.[/color]

    What is a RWList?
    What is a RWListIter?

    Without that information noone is able to compile the above
    code snippets to see what is going on.

    BTW: There is no function Main() in C++. Only main() and it
    has to return an int in any case. There is no longer an implicite
    int rule in C++.

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • atandon

      #3
      Re: RWList pass by reference -probkem

      Hi Sangeetha,

      You are storing the objects of class A in the list as values and not as
      pointers. If you do a simple change and store pointers instead of just
      values of objects then you will get the desired output.

      Please look at the following code:
      class A
      {
      public:
      const RWCString & x() {return _x;}
      const RWCString & y() {return _y;}
      void x(const RWCString & value) { _x = value; }
      void y(const RWCString & value) { _y = value; }

      private:
      RWCString _x;
      RWCString _y;
      };

      void f (RWTValSlist<A* >& alist) // receiving as reference of that list
      {
      RWTValSlistIter ator<A* > AIter(alist);

      while (AIter())
      {
      A &a = *(AIter.key()); // storing as reference
      a.x("xxxxxxxxx" );
      }

      }

      void main ()
      {
      RWTValSlist<A* > alist;
      A a1,a2;

      a1.y("something ");
      a2.y("asdasdasa ");

      alist.insert(&a 1);
      alist.insert(&a 2);

      f(alist);

      RWTValSlistIter ator<A* > AIter(alist);
      while (AIter())
      {
      A &a = *(AIter.key()); // storing as reference
      cout<<"x = "<<a.x()<<e ndl;
      }

      }

      As far as I think you are passing the list as reference to a function.
      That is fine. But the values in it are still be copied internally. As far
      as I think this might be the reason.

      Try the following program also
      void f(int temp)
      {
      int &temp_1 = temp;
      temp_1 = 100;
      }

      main()
      {
      int t = 1000;
      f(t);
      cout<<t;
      }

      Here there will be no change in the value of t and it remains 1000.

      If you have some issues then we can discuss it on this newsgroup.

      Comment

      • Thomas Matthews

        #4
        Re: RWList pass by reference -probkem

        sangeetha wrote:[color=blue]
        > Hello,
        >
        > I'm passing a RWList object of one complex data to function f(). In
        > the function f() i'm receiving in that list as reference. In the
        > function f(), i want to modify one member variable in all the nodes in
        > that input RWList. I've written following (dummy)code to do produce my
        > problem. The values modified in the function are not reflected in the
        > called function. I spend sometime not able to find/resolve the
        > problem...
        >
        > class A{
        > public:
        > const RWString & x( return _x;}
        > const RWstring & y( return _y;}
        > void x(const RWStrng & value) { _x = value; }
        > void y(const RWStrng & value) { _y = value; }
        >
        > private:
        > RWString _x;
        > RWString _y;
        > };[/color]

        Your class could be simplified by:
        1. declaring _x and _y as public.
        or 2. changing it to a struct.

        [color=blue]
        >
        > void f ( RWList<A>& alist) // receiving as reference of that list
        > {
        > RWListIter<A> AIter(alist);
        >
        > while (AIter())
        > {
        > A &a = AIter.key(); // storing as reference
        > a.x("xxxxxxxxx" );
        > }
        >
        > }
        > Main ()[/color]

        The C++ language is case-sensitive.
        This should be:
        int main(void)

        [color=blue]
        > {
        > RWList<A> alist;
        > A a1,a2;
        > ...
        > a1.y("something ");
        > a2.y("asdasdasa ");
        > ...
        > alist.insert(a1 );
        > alist.inset(a2) ;[/color]
        Is this alist.insert(a2 );
        What does the inset() method do?

        [color=blue]
        > ...
        > f(alist);
        > ...[/color]

        The main() function must return a value.
        Try EXIT_SUCCESS or EXIT_FAILURE as defined
        in <cstdlib>.
        [color=blue]
        > }
        >
        >
        > Please correct if anything wrong in my code.
        >
        > It appears that begin and end function are not provided in the RWList
        > class. ( I tried AList.begin() and AList.end() )[/color]

        If you want a begin() and end() method of a linked list, try
        the std::list.
        [color=blue]
        >
        > Thanks,
        > Sangeetha.[/color]



        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book
        http://www.sgi.com/tech/stl -- Standard Template Library

        Comment

        • Karl Heinz Buchegger

          #5
          Re: RWList pass by reference -probkem

          atandon wrote:[color=blue]
          >
          > Hi Sangeetha,
          >
          > You are storing the objects of class A in the list as values and not as
          > pointers. If you do a simple change and store pointers instead of just
          > values of objects then you will get the desired output.[/color]

          Changing from storing values to storing pointers is never *a simple change*.
          It opens another can of worms.

          In any way I don't think that this is a solution to the real problem.
          I think the real problem is that the key() function doesn't do what
          the OP thinks it does. But without knowing what a RWList is and how
          RWListIter works, it is impossible to tell.
          [color=blue]
          >
          > As far as I think you are passing the list as reference to a function.
          > That is fine. But the values in it are still be copied internally.[/color]

          If something is passed by reference, then nothing is copied at all.
          A reference to the original object (the List in this case) is created
          and that's it.
          [color=blue]
          > As far
          > as I think this might be the reason.
          >
          > Try the following program also
          > void f(int temp)
          > {
          > int &temp_1 = temp;
          > temp_1 = 100;
          > }
          >
          > main()
          > {
          > int t = 1000;
          > f(t);
          > cout<<t;
          > }
          >
          > Here there will be no change in the value of t and it remains 1000.[/color]

          This is exactly what I suspect key() to do:
          Not returning a reference, but returning a copy of the stored value
          in the List.

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • atandon

            #6
            Re: RWList pass by reference -probkem

            Hi Sangeetha,

            You are storing the objects of class A in the list as values and not as
            pointers. If you do a simple change and store pointers instead of just
            values of objects then you will get the desired output.

            Please look at the following code:
            class A
            {
            public:
            const RWCString & x() {return _x;}
            const RWCString & y() {return _y;}
            void x(const RWCString & value) { _x = value; }
            void y(const RWCString & value) { _y = value; }

            private:
            RWCString _x;
            RWCString _y;
            };

            void f (RWTValSlist<A* >& alist) // receiving as reference of that list
            {
            RWTValSlistIter ator<A* > AIter(alist);

            while (AIter())
            {
            A &a = *(AIter.key()); // storing as reference
            a.x("xxxxxxxxx" );
            }

            }

            void main ()
            {
            RWTValSlist<A* > alist;
            A a1,a2;

            a1.y("something ");
            a2.y("asdasdasa ");

            alist.insert(&a 1);
            alist.insert(&a 2);

            f(alist);

            RWTValSlistIter ator<A* > AIter(alist);
            while (AIter())
            {
            A &a = *(AIter.key()); // storing as reference
            cout<<"x = "<<a.x()<<e ndl;
            }

            }

            As far as I think you are passing the list as reference to a function.
            That is fine. But the values in it are still be copied internally. As far
            as I think this might be the reason.

            Try the following program also
            void f(int temp)
            {
            int &temp_1 = temp;
            temp_1 = 100;
            }

            main()
            {
            int t = 1000;
            f(t);
            cout<<t;
            }

            Here there will be no change in the value of t and it remains 1000.

            If you have some issues then we can discuss it on this newsgroup.

            Comment

            • sangeetha

              #7
              Re: RWList pass by reference -probkem

              RogueWave(RW) STL library
              (LIST => RWList, List::Iterator => RWListIter)


              Karl Heinz Buchegger <kbuchegg@gasca d.at> wrote in message news:<40BDC67D. E14C0D0D@gascad .at>...[color=blue]
              > sangeetha wrote:[color=green]
              > >
              > > Hello,
              > >
              > > I'm passing a RWList object of one complex data to function f(). In
              > > the function f() i'm receiving in that list as reference. In the
              > > function f(), i want to modify one member variable in all the nodes in
              > > that input RWList. I've written following (dummy)code to do produce my
              > > problem. The values modified in the function are not reflected in the
              > > called function. I spend sometime not able to find/resolve the
              > > problem...
              > >
              > > class A{
              > > public:
              > > const RWString & x( return _x;}
              > > const RWstring & y( return _y;}
              > > void x(const RWStrng & value) { _x = value; }
              > > void y(const RWStrng & value) { _y = value; }
              > >
              > > private:
              > > RWString _x;
              > > RWString _y;
              > > };
              > >
              > > void f ( RWList<A>& alist) // receiving as reference of that list
              > > {
              > > RWListIter<A> AIter(alist);
              > >
              > > while (AIter())
              > > {
              > > A &a = AIter.key(); // storing as reference
              > > a.x("xxxxxxxxx" );
              > > }[/color]
              >[color=green]
              > > }
              > > Main ()
              > > {
              > > RWList<A> alist;
              > > A a1,a2;
              > > ...
              > > a1.y("something ");
              > > a2.y("asdasdasa ");
              > > ...
              > > alist.insert(a1 );
              > > alist.inset(a2) ;
              > > ...
              > > f(alist);
              > > ...
              > > }
              > >
              > > Please correct if anything wrong in my code.[/color]
              >
              > What is a RWList?
              > What is a RWListIter?
              >
              > Without that information noone is able to compile the above
              > code snippets to see what is going on.
              >
              > BTW: There is no function Main() in C++. Only main() and it
              > has to return an int in any case. There is no longer an implicite
              > int rule in C++.[/color]

              Comment

              Working...