* Jianwei Sun:
Hello AnonMail2005@gmail.com,
On Aug 10, 11:18 am, Jianwei Sun <jsunnewsgr...@gmail.com> wrote:
I am reading a peice of code which is at the following link:
http://www.brpreiss.com/books/opus4/html/page141.html#SECTION00712
30 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/html/page98.html#SECTION0052100
00 ...- 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
Perhaps the author is very knowledgable about data structures and
algorithms, but just doesn't know even the basics about C++ and
programming in general?
For the code isn't even syntactically correct and should not compile,
and the use of a reference here is extremely non-idiomatic and
misleading, but the basic algorith and data structure logic could be
sound: it seems that it could work, if expressed correctly, but that
could be like interpreting as favorably as possible the writings of a
chimpanzee posing as a college professor.
So, I'm pretty sure that if you look up the ACCU C++-oriented review
of that book it will be very far from 5 stars: on the other end of the
scale. ;-)
Cheers, & hth.,
- Alf
Thank you, and I like "that could be like interpreting as favorably as possible
chimpanzee posing as a college professor.".
However, I will still read this book, with more attention to the c++ code.