RWList pass by reference -probkem

S

sangeetha

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

Karl Heinz Buchegger

sangeetha said:
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.

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

atandon

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
{
RWTValSlistIterator<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(&a1);
alist.insert(&a2);

f(alist);

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

}

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

Thomas Matthews

sangeetha said:
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;
};

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

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 ()

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

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

...
f(alist);
...

The main() function must return a value.
Try EXIT_SUCCESS or EXIT_FAILURE as defined
in said:
}


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() )

If you want a begin() and end() method of a linked list, try
the std::list.
Thanks,
Sangeetha.



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
K

Karl Heinz Buchegger

atandon said:
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.

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

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

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

atandon

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
{
RWTValSlistIterator<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(&a1);
alist.insert(&a2);

f(alist);

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

}

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.
 

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
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top