Re: Question about this piece of code.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jianwei Sun

    Re: Question about this piece of code.

    Hello Alf,
    * Jianwei Sun:
    >
    >Hello AnonMail2005@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/...l#SECTION00712
    >>>>30 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.Extrac t(&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/...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
    the writings of a
    chimpanzee posing as a college professor.".

    However, I will still read this book, with more attention to the c++ code.

    J.W.


Working...