By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

RWList pass by reference -probkem

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
sangeetha wrote:

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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
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.

Jul 22 '05 #3

P: n/a
sangeetha wrote:
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 <cstdlib>.
}
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

Jul 22 '05 #4

P: n/a
atandon wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

P: n/a
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.

Jul 22 '05 #6

P: n/a
RogueWave(RW) STL library
(LIST => RWList, List::Iterator => RWListIter)
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<40***************@gascad.at>...
sangeetha wrote:

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

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.