473,503 Members | 5,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about this piece of code.


I am reading a peice of code which is at the following link:
http://www.brpreiss.com/books/opus4/...00000000000000

The code is like this:

Object& StackAsLinkedList::Pop()
{
if(count==0)
throw domain_error("stack is empty");

Object& const result=*list.First();
list.Extract(&result);
--count;
return result;
}

Does this code return a reference to local variable result? If this is the
case, then this code
has problem?

Thanks,
J.W.
Aug 10 '08 #1
6 1285

I am reading a peice of code which is at the following link:
http://www.brpreiss.com/books/opus4/...CTION007123000
000000000000

The code is like this:

Object& StackAsLinkedList::Pop()
{
if(count==0)
throw domain_error("stack is empty");
Object& const result=*list.First();
list.Extract(&result);
--count;
return result;
}
Does this code return a reference to local variable result? If this is
the case, then this code has problem?

Thanks,
J.W.
The list extract function is here:

http://www.brpreiss.com/books/opus4/...00000000000000
Aug 10 '08 #2
On Aug 10, 11:12 am, Jianwei Sun <jsunnewsgr...@gmail.comwrote:
I am reading a peice of code which is at the following link:http://www.brpreiss.com/books/opus4/...ECTION00712300...

The code is like this:

Object& StackAsLinkedList::Pop()
{
if(count==0)
throw domain_error("stack is empty");

Object& const result=*list.First();
list.Extract(&result);
--count;
return result;

}

Does this code return a reference to local variable result? If this is the
case, then this code
has problem?
the function returns a reference (as required since the function's
return type is Object&).
The declaration for result is a const reference:
Object& const result(*list.First());
In effect, you aren't dealing with a temporary variable. Passing
references by value doesn't magicly allocate or deallocate its
referant.

Whether the code is correct is unknown to us. In particular, no-one
here knows what happens to result (the Object) during and after the
call to list.Extract(&result). We have no way of knowing how (or if)
the list is managing the Object lifetimes.
Aug 10 '08 #3
Hello Salt_Peter,
On Aug 10, 11:12 am, Jianwei Sun <jsunnewsgr...@gmail.comwrote:
>I am reading a peice of code which is at the following
link:http://www.brpreiss.com/books/opus4/...tml#SECTION007
12300...

The code is like this:

Object& StackAsLinkedList::Pop()
{
if(count==0)
throw domain_error("stack is empty");
Object& const result=*list.First();
list.Extract(&result);
--count;
return result;
}

Does this code return a reference to local variable result? If this
is the
case, then this code
has problem?
the function returns a reference (as required since the function's
return type is Object&).
The declaration for result is a const reference:
Object& const result(*list.First());
In effect, you aren't dealing with a temporary variable. Passing
references by value doesn't magicly allocate or deallocate its
referant.
Whether the code is correct is unknown to us. In particular, no-one
here knows what happens to result (the Object) during and after the
call to list.Extract(&result). We have no way of knowing how (or if)
the list is managing the Object lifetimes.
Hi, Peter,

The extact function is here:

http://www.brpreiss.com/books/opus4/...00000000000000

If I get your meaning correclty, if the extract is deleting this node, then
this won't be correct.
If the extact doesn't delete this node, this will work fine.

Thanks,
J.W.
Aug 10 '08 #4
On Aug 10, 11:18*am, Jianwei Sun <jsunnewsgr...@gmail.comwrote:
I am reading a peice of code which is at the following link:
http://www.brpreiss.com/books/opus4/...CTION007123000
000000000000
The code is like this:
Object& StackAsLinkedList::Pop()
{
if(count==0)
throw domain_error("stack is empty");
Object& const result=*list.First();
list.Extract(&result);
--count;
return result;
}
Does this code return a reference to local variable result? If this is
the case, then this code has problem?
Thanks,
J.W.

The list extract function is here:

http://www.brpreiss.com/books/opus4/...210000...-Hide quoted text -

- Show quoted text -
If we ignore that result is a const and the return type is non-const,
it would seem that the returned value (a reference) refers to an
object that has been deleted inside the Extract function. This, of
course, is wrong.
Aug 10 '08 #5
Hello An**********@gmail.com,
On Aug 10, 11:18 am, Jianwei Sun <jsunnewsgr...@gmail.comwrote:
>>I am reading a peice of code which is at the following link:
http://www.brpreiss.com/books/opus4/...SECTION0071230
00 000000000000

The code is like this:

Object& StackAsLinkedList::Pop()
{
if(count==0)
throw domain_error("stack is empty");
Object& const result=*list.First();
list.Extract(&result);
--count;
return result;
}
Does this code return a reference to local variable result? If this
is
the case, then this code has problem?
Thanks,
J.W.
The list extract function is here:

http://www.brpreiss.com/books/opus4/...CTION005210000
...- Hide quoted text -

- Show quoted text -
If we ignore that result is a const and the return type is non-const,
it would seem that the returned value (a reference) refers to an
object that has been deleted inside the Extract function. This, of
course, is wrong.
Thank you for verify, I just want to make sure I didn't miss something obvious
since the author who maintains
this tutorial is a very knowledgeable person and this book is a 5-star book
on amazon.com

Thanks,
J.W.
Aug 10 '08 #6
On 10 Aug, 19:56, Jianwei Sun <jsunnewsgr...@gmail.comwrote:
Thank you for verify, I just want to make sure I didn't miss something obvious
since the author who maintains
this tutorial is a very knowledgeable person and this book is a 5-star book
on amazon.com

Thanks,
J.W
Still, I think the author's approach is inherently flawed. Why would
you want to implement your own data structures based on storing
pointers to an Object base class in C++? That's more like pre-generics
Java than idiomatic C++.

For C++, I recommend reading up on the STL instead.

DP
Aug 11 '08 #7

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

Similar topics

2
992
by: Woody Splawn | last post by:
I have been using VB.net for stand alone and client/server kinds of things but am beginning to explore its use for web applications. I need to ask a couple of real basic questions. My...
29
3537
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
73
4206
by: JoeC | last post by:
I am writing a game and I am having a challenge with my combat function. All I want to do is find out how to group pieces that are in the same space. There are two sides and all the units that...
5
2229
by: DarkPhoenix739 | last post by:
I have 3 classes: a Windows Form, a Game, and a Piece. The Game has a Piece, and the Windows Form has a Game. The Windows Form needs to be able to "get" the game's Piece, and view its data, but not...
30
3472
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
7194
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
7070
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
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
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...
1
6976
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
7449
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
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.