473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Jul 22 '05 #1
6 1422
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
19
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
3
by: Scott M. | last post by:
If I pass a reference type ByVal, am I making a copy of the object on the heap or am I making a copy of a pointer to the object on the heap? If I pass a string object (reference type) into a sub...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.