473,386 Members | 1,796 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,386 software developers and data experts.

STL lists as retrun value

Hi,

here is my small program:

Header file:

Class myClass
{
public:
void function();
list< pair< Object1*, Object1*> > getList() const;
...
private:

typedef std::pair< Object1*, Object1* > mPairType;
list< mPairType > myList;
...
};
And the source file:

list< pair< Object1*, Object1*> > myClass:getList() const
{
return myList;
}
void myClass::function()
{
typedef std::pair< Object1*, Object1* > myType;
list< myType>::const_iterator it = getList().begin();

for ( ; it != getList().end(); ++it )
{
std::pair< Object1*, Object1* > myPairs = (*it);
}
}
....
When I run this program, I get segmentation faults in line
"std::pair< Object1*, Object1* > myPairs = (*it);".
When I instead replace the function getList() by a direct
access to member "myList", i.e.:

void myClass::function()
{
typedef std::pair< Object1*, Object1* > myType;
list< myType>::const_iterator it = myList.begin();

for ( ; it != myList.end(); ++it )
{
std::pair< Object1*, Object1* > myPairs = (*it);
}
}
Everything works fine.

I don't understand why the return value of getList() [ returns the STL list ]
is not the same as a direct access to the private class member "myList".

What do I have to change to access the private member function "myList"
by the public class function getList() ?

I appreciate any help.

Thank you.

Chris
Feb 3 '06 #1
4 1678
Christian Christmann wrote:
here is my small program:
It's not a program. It's lacking some important elements, which prevents
it from being a program. Next time either post _complete_ code, or don't
claim it's a program. Below is a *code fragment*.

Header file:

Class myClass
I suppose it is really

class myClass
{
public:
void function();
list< pair< Object1*, Object1*> > getList() const;
...
private:

typedef std::pair< Object1*, Object1* > mPairType;
list< mPairType > myList;
...
};
And the source file:

list< pair< Object1*, Object1*> > myClass:getList() const
{
return myList;
Here you're duplicating (copying) your list. Is it really necessary?
You could just return a const reference to it, no?
}
void myClass::function()
{
typedef std::pair< Object1*, Object1* > myType;
You already have a very similar typedef in your class. Why duplicate?
Just use 'mPairType' here.
list< myType>::const_iterator it = getList().begin();
'getList()' creates a temporary, which is _destroyed_ at the end of this
initialisation. 'it' is _invalid_ as soon as you reach the semicolon.

for ( ; it != getList().end(); ++it )
{
std::pair< Object1*, Object1* > myPairs = (*it);
}
}
...
When I run this program, I get segmentation faults in line
"std::pair< Object1*, Object1* > myPairs = (*it);".
See above.
When I instead replace the function getList() by a direct
access to member "myList", i.e.:

void myClass::function()
{
typedef std::pair< Object1*, Object1* > myType;
list< myType>::const_iterator it = myList.begin();
Of course. Now, the iterator is valid because the list itself is _still_
valid at this point (and as long as the 'myClass' object survives).

for ( ; it != myList.end(); ++it )
{
std::pair< Object1*, Object1* > myPairs = (*it);
}
}
Everything works fine.

I don't understand why the return value of getList() [ returns the STL list ]
is not the same as a direct access to the private class member "myList".

What do I have to change to access the private member function "myList"
by the public class function getList() ?


V
--
Please remove capital As from my address when replying by mail
Feb 3 '06 #2
On Fri, 03 Feb 2006 16:35:59 +0100, Christian Christmann
<pl*****@yahoo.de> wrote:
Hi,

here is my small program:

Header file:

Class myClass
{
public:
void function();
list< pair< Object1*, Object1*> > getList() const;
...
private:

typedef std::pair< Object1*, Object1* > mPairType;
list< mPairType > myList;
...
};
And the source file:

list< pair< Object1*, Object1*> > myClass:getList() const
{
return myList;
}
void myClass::function()
{
typedef std::pair< Object1*, Object1* > myType;
list< myType>::const_iterator it = getList().begin();

for ( ; it != getList().end(); ++it )
{
std::pair< Object1*, Object1* > myPairs = (*it);
}
}
...
When I run this program, I get segmentation faults in line
"std::pair< Object1*, Object1* > myPairs = (*it);".
When I instead replace the function getList() by a direct
access to member "myList", i.e.:

void myClass::function()
{
typedef std::pair< Object1*, Object1* > myType;
list< myType>::const_iterator it = myList.begin();

for ( ; it != myList.end(); ++it )
{
std::pair< Object1*, Object1* > myPairs = (*it);
}
}
Everything works fine.

I don't understand why the return value of getList() [ returns the STL list ]
is not the same as a direct access to the private class member "myList".

What do I have to change to access the private member function "myList"
by the public class function getList() ?

I appreciate any help.


Not too surprising, considering that getList() returns a temporary
list. Therefore, in this statement:

list< myType>::const_iterator it = getList().begin();

"it" is an iterator to a list which goes out of scope as soon as the
function call begin() returns.

--
Bob Hairgrove
No**********@Home.com
Feb 3 '06 #3
Thank you for your answer.
list< myType>::const_iterator it = getList().begin();
'getList()' creates a temporary, which is _destroyed_ at the end of this
initialisation. 'it' is _invalid_ as soon as you reach the semicolon.


Now I see that 'getList()' returns a temporary copy of the list. But why
is the list destroyed at the end of the initialization?
In my opinition, the iterator 'it' should point to an empty list that has
just been created, but the list should still exist since there's still a
reference to it.

Chris
Feb 4 '06 #4
On Sat, 04 Feb 2006 12:57:05 +0100, Christian Christmann
<pl*****@yahoo.de> wrote:
Thank you for your answer.
list< myType>::const_iterator it = getList().begin();

'getList()' creates a temporary, which is _destroyed_ at the end of this
initialisation. 'it' is _invalid_ as soon as you reach the semicolon.


Now I see that 'getList()' returns a temporary copy of the list. But why
is the list destroyed at the end of the initialization?
In my opinition, the iterator 'it' should point to an empty list that has
just been created, but the list should still exist since there's still a
reference to it.


An iterator is never a reference because references cannot be
incremented, decremented or changed by adding or subtracting a value
in the way an iterator can be.

An iterator is more like a pointer, but it is also not really a
pointer, either. It is an object. Often, though, an iterator will be
implemented similarly to a pointer, although that isn't possible for
std::list (do you kno why?) But conceptually, you might think of it in
this particular instance as a kind of pointer.

Assume that begin() returned a pointer instead of an iterator. When
the object it points to is destroyed, you would have a dangling
pointer. You can also have a dangling iterator for similar reasons.

--
Bob Hairgrove
No**********@Home.com
Feb 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lorn | last post by:
I'm trying to figure out a way to create dynamic lists or possibly antother solution for the following problem. I have multiple lines in a text file (every line is the same format) that are...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
7
by: ad | last post by:
How to retrun a character with ascii code? It is useChr(65) return 'A' in VB.NET. What is the equivalent function in C#?
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
2
by: V_S_H_Satish | last post by:
Dear Friends I am working as oracle and ms sql dba from last 4 years. My company recently migrated to DB2 databases. So i am very much new to db2 database Can any one pls provide script to...
11
by: JJLaRocque | last post by:
Hi all, Is there a simple python function to return the list index of the minimum entry in a list of lists? ie, for , , ] to return 2,4. Or, same question but just for a list of numbers, not a...
8
by: Ed Dror | last post by:
Hi there ASP.NET 2.0 VB & SQL Express Lest take Northwind Categories Products as example I create a table that hold these two together and I create a stored procedure like select ProductID,...
48
by: coool | last post by:
Hi I'm now trying to have a dependent lists in a form my form is a query based form i.e. I fill my MySQL query from this form I have around 30 fields/columns
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.