dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: My difficulty in this section

  1. #1
    Join Date
    May 2014
    Posts
    72

    My difficulty in this section

    Hi,

    I read some part a C++ book explaining lists. But I think the author has mistaken in that. If possible, please read section 17.9.3. An example: Lists, of this link http://books.google.com/books?id=We2...epage&q&f=true.

    I consider the predecessor (pred) as prior Link and successor (succ) as next Link. And the direction of expand to be from left to write. That is, if we have Link l1, the next Link, l2, will be new and therefore, in the right side of the previous Link, l1.

    I think there is some mistake or wrong replacement in places of prep & succ and also in the code. Can you understand that section and you think it has not any wrong?

    Thanks.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: My difficulty in this section

    Quote Originally Posted by abbassi View Post
    I consider the predecessor (pred) as prior Link and successor (succ) as next Link. And the direction of expand to be from left to write. That is, if we have Link l1, the next Link, l2, will be new and therefore, in the right side of the previous Link, l1.
    Correct.

    Quote Originally Posted by abbassi View Post
    I think there is some mistake or wrong replacement in places of prep & succ and also in the code. Can you understand that section and you think it has not any wrong?
    i do not see any mistake here.
    Note that there is a double-linked list, so you can construct it to both "left-to-right" and "right-to-left" directions.
    Victor Nijegorodov

  3. #3
    Join Date
    May 2014
    Posts
    72

    Re: My difficulty in this section

    You are right.
    At the third read, finally I found it has no mistakes. Thank you very much.
    Sorry if I took your time.

  4. #4
    Join Date
    May 2014
    Posts
    72

    Re: My difficulty in this section

    Would you please have a look again at that book?
    In page 617 he has written this:

    Code:
    Link * p = find(norse_gods,"Zeus");
    if(p) {
            erase(p);
            insert(greek_gods,p);
    }
    Apart from the bug he has mentioned in the book, there are still others.
    Both of the erase and insert functions have returning type, which is a Link*, but in that snip code, they haven't been assigned to a variable.
    Still correct!?

    Thanks.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: My difficulty in this section

    Quote Originally Posted by abbassi View Post
    Code:
    Link * p = find(norse_gods,"Zeus");
    if(p) {
            erase(p);
            insert(greek_gods,p);
    }
    Apart from the bug he has mentioned in the book, there are still others.
    Both of the erase and insert functions have returning type, which is a Link*, but in that snip code, they haven't been assigned to a variable.
    Still correct!?
    It is not mandatory to assign the returned pointers to some variables.
    Victor Nijegorodov

  6. #6
    Join Date
    May 2014
    Posts
    72

    Re: My difficulty in this section

    Thank you.

    Is it this way just for functions returning a pointer or also for other returning types please?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,969

    Re: My difficulty in this section

    Basically a function can return any required type - although for large amounts of data the copy-constructor for the returned type for a value (as opposed to a pointer or a ref) could impose a significant performance overhead.

    See http://www.learncpp.com/cpp-tutorial...e-and-address/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  8. #8
    Join Date
    May 2014
    Posts
    72

    Re: My difficulty in this section

    Thank you for your explanations. But I asked about not assigning the returned value of a function.
    For example if we have:

    int f() { return 6; }

    and in main() function we use it:

    int main() {
    f();
    return 0;
    }

    Doesn't it have any problem? Is it a safe coding?

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: My difficulty in this section

    Yes, it is "safe", although the function f() itself in this example does not make any sense.
    Victor Nijegorodov

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,969

    Re: My difficulty in this section

    Any value returned by a function can be ignored if not required. This is valid
    Code:
    int r = f();
    f();
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  11. #11
    Join Date
    May 2014
    Posts
    72

    Re: My difficulty in this section

    Thank you all very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)