Question about this piece of code.

J

Jianwei Sun

I am reading a peice of code which is at the following link:
http://www.brpreiss.com/books/opus4/html/page141.html#SECTION007123000
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/html/page98.html#SECTION0052100000000000000000
 
S

Salt_Peter

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

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

Jianwei Sun

Hello Salt_Peter,
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/html/page98.html#SECTION0052100000000000000000

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

Jianwei Sun

Hello (e-mail address removed),
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.
 
T

Triple-DES

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top