473,655 Members | 3,056 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& StackAsLinkedLi st::Pop()
{
if(count==0)
throw domain_error("s tack is empty");

Object& const result=*list.Fi rst();
list.Extract(&r esult);
--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 1298

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& StackAsLinkedLi st::Pop()
{
if(count==0)
throw domain_error("s tack is empty");
Object& const result=*list.Fi rst();
list.Extract(&r esult);
--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& StackAsLinkedLi st::Pop()
{
if(count==0)
throw domain_error("s tack is empty");

Object& const result=*list.Fi rst();
list.Extract(&r esult);
--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.Fi rst());
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(&r esult). 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& StackAsLinkedLi st::Pop()
{
if(count==0)
throw domain_error("s tack is empty");
Object& const result=*list.Fi rst();
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.Fi rst());
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(&r esult). 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& StackAsLinkedLi st::Pop()
{
if(count==0)
throw domain_error("s tack is empty");
Object& const result=*list.Fi rst();
list.Extract(&r esult);
--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**********@gm ail.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& StackAsLinkedLi st::Pop()
{
if(count==0 )
throw domain_error("s tack is empty");
Object& const result=*list.Fi rst();
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
1002
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 understanding is that if you are going to write an application for the web using VS.net you need to have a web server. I suppose what is meant by this is that you need to have a seperate machine (PC) connected to the web who's sole purpose is to get and...
29
3563
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 one data field - but i'm not sure) :-) Background info:
73
4260
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 are in the same space fight. I want to add up the attack factors and defending factors in the same space then figure out the odds so I can roll against an odds table. Basically each piece holds its own x and y loc. Here is what I have right...
5
2233
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 have access to the functions of Piece that modify that data. For Example: ..Windows Form .....Game .......Piece .........Location .........public location {get {return Location;} } .........public Move() .......public piece {get { return...
30
3513
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
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.