v2 Pointers and polymorphism explained [preview, PDF, part of my attempted "Correct C++ Tutorial"]

Discussion in 'C++' started by Alf P. Steinbach, Oct 28, 2005.

  1. * Alf P. Steinbach:
    >
    > <url: http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha.doc.pdf>.


    Thanks to all who commented on the 1st version: Robert Macy, Chris Val,
    BobR, and Sigurd Stenersen (I hope I haven't forgotten anybody!).

    Most of this is now fixed, I think:

    * Definition of "pointer" (more details added).
    * Definition of "dereference" (better focused).
    * Better more detailed discussion of const and const correctness.
    * The bad pointer 2 UB program "improved" to be more overtly UB.
    * The vtable figure improved (alas OpenOffice makes figures look bad).
    * Renamed a member function for C++-oriented versions of the example
    program in section 1.2, added discussion.
    * Completed section 1.3.1 about ensuring dynamic allocation.

    And regarding 1.3.1 I think I like this method better than the one in
    the FAQ, even though this method is theoretically a more limited one
    than the FAQ's completely general method (one line per class is much
    better than a number of factory functions per class, I think!).

    Updated version at:
    <url:
    http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha2.doc.pdf>.

    Further comments are welcome! ;-)

    Contents (so far) -- the page numbers are probably incorrect since
    this was for the original version, but the sections are the same:

    > 1 1 Pointers.
    > 1 1.1 Introduction to the basics.
    > 1 1.1.1 How to obtain a pointer to a given object, and how to use that pointer.
    > 3 1.1.2 The nullpointer value, valid and invalid pointers.
    > 6 1.1.3 How to implement out-arguments by using pointers.
    > 8 1.1.4 How to implement in-arguments by using pointers.
    > 9 1.1.5 How to use C++ style references instead of C style pointers for arguments.
    > 11 1.1.6 How to access main arguments.
    > 12 1.1.7 Const-correctness for pointers (and references).
    > 14 1.2 Run-time polymorphism.
    > 14 1.2.1 Polymorphism.
    > 16 1.2.2 The concept of linked data structures.
    > 17 1.2.3 C-style polymorphism with simulated dynamic types (just one actual type).
    > 19 1.2.4 Basic use of dynamic memory allocation & deallocation.
    > 22 1.2.5 How to use function pointers to simulate dynamic types, C-style.
    > 25 1.2.6 Using real types and inheritance for logical sub-types, mostly C-style.
    > 32 1.2.7 Using vtables, mostly C-style (also a few words about abstract classes, etc.).
    > 38 1.2.8 C++ virtual member functions.
    > 45 1.2.9 C++ destructors and polymorphic delete.
    > 49 1.2.10 Object ownership and the C++ std::auto_ptr.
    > 53 1.2.11 C++ constructors and exception safety for new.
    > 57 1.3 Dynamically allocated objects versus others.
    > 58 1.3.1 Ensure dynamic allocation using C++ access specifiers and friendship.
    > 61 – End Of Text –


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 28, 2005
    #1
    1. Advertising

  2. Pointers and polymorphism explained [beta preview, PDF, part of my attempted "Correct C++ Tutorial"]

    Again I ask for comments, corrections, praise, damnations, nits, etc.!

    Of the new stuff added since the alpha3 version I think most interesting
    is perhaps the section on how to ensure that smart pointers are used.

    By discussing that I'm taking a leap, for as far as I know the technique
    hasn't been used in production (I've never seen it discussed or used),
    so comments about this would especially appreciated. E.g., are there
    problems using that with Loki's small object allocator? I don't know,
    but I think it appears to be so useful that it deserves discussion, even
    if it turns out there are restrictions for very special usage contexts.


    PDF document:
    <url:
    http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01_beta.doc.pdf>

    Examples source code:
    <url:
    http://home.no.net/dubjai/win32cpptut/special/pointers/preview/examples.zip>

    Contents:
    Please see the PDF document's table of contents.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 21, 2005
    #2
    1. Advertising

  3. Alf P. Steinbach

    Vince Morgan Guest

    Re: Pointers and polymorphism explained [beta preview, PDF, part of my attempted "Correct C++ Tutorial"]

    "Alf P. Steinbach" <> wrote in message
    news:...
    > Again I ask for comments, corrections, praise, damnations, nits, etc.!
    >
    > Of the new stuff added since the alpha3 version I think most interesting
    > is perhaps the section on how to ensure that smart pointers are used.
    >
    > By discussing that I'm taking a leap, for as far as I know the technique
    > hasn't been used in production (I've never seen it discussed or used),
    > so comments about this would especially appreciated. E.g., are there
    > problems using that with Loki's small object allocator? I don't know,
    > but I think it appears to be so useful that it deserves discussion, even
    > if it turns out there are restrictions for very special usage contexts.
    >
    >
    > PDF document:
    > <url:
    >

    http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01_beta.doc.pdf>
    >
    > Examples source code:
    > <url:
    >

    http://home.no.net/dubjai/win32cpptut/special/pointers/preview/examples.zip>
    >
    > Contents:
    > Please see the PDF document's table of contents.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q:


    I think you have done an excellent job from what I have read so far Alf.
    I've a way to go to finish chapter one, but I like the style of this very
    much.
    Not too verbose, but certainly not lacking in substance either.
    The © won't do you any good in this case. I'm keeping it!

    --
    Vince Morgan
    Remove UNSPAM
    at is the most annoying thing on usenet and in
    e-mail?
    Vince Morgan, Nov 28, 2005
    #3
  4. Alf P. Steinbach

    Vince Morgan Guest

    Re: Pointers and polymorphism explained [beta preview, PDF, part of my attempted "Correct C++ Tutorial"]

    "Alf P. Steinbach" <> wrote in message
    news:...
    > Again I ask for comments, corrections, praise, damnations, nits, etc.!
    >
    > Of the new stuff added since the alpha3 version I think most interesting
    > is perhaps the section on how to ensure that smart pointers are used.
    >
    > By discussing that I'm taking a leap, for as far as I know the technique
    > hasn't been used in production (I've never seen it discussed or used),
    > so comments about this would especially appreciated. E.g., are there
    > problems using that with Loki's small object allocator? I don't know,
    > but I think it appears to be so useful that it deserves discussion, even
    > if it turns out there are restrictions for very special usage contexts.
    >
    >
    > PDF document:
    > <url:
    >

    http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01_beta.doc.pdf>
    >

    The section: Obtaining a pointer to(also called address of) a given object.
    Half way through second paragraph.
    <quote>
    "However, with the C++98 standard it's not always formally the case that &*p
    is

    identically the same as writing just p, because the address might not permit
    dereferencing. And

    when p is a variable, as above, p is an lvalue that can be assigned to,
    whereas &*p yields an rvalue."

    </quote>

    Did you mean *&p as opposed to "&*p"?

    > Examples source code:
    > <url:
    >

    http://home.no.net/dubjai/win32cpptut/special/pointers/preview/examples.zip>
    >
    > Contents:
    > Please see the PDF document's table of contents.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?




    Vince Morgan
    Remove UNSPAM
    Vince Morgan, Nov 28, 2005
    #4
  5. Re: Pointers and polymorphism explained [beta preview, PDF, part of my attempted "Correct C++ Tutorial"]

    * Vince Morgan:
    > > PDF document:
    > > <url:
    > > http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01_beta.doc.pdf>
    > >

    > The section: Obtaining a pointer to(also called address of) a given object.
    > Half way through second paragraph.
    > <quote>
    > "However, with the C++98 standard it's not always formally the case
    > that &*p is identically the same as writing just p, because the address
    > might not permit dereferencing. And when p is a variable, as above, p is
    > an lvalue that can be assigned to, whereas &*p yields an rvalue."
    > </quote>
    >
    > Did you mean *&p as opposed to "&*p"?


    No.

    First case. p is a pointer. If the pointer value permits dereferencing
    (e.g., is not a nullpointer), then *p gives you what p points to. &*p
    then gives you the address of that object, i.e. the same value as p, but
    only if the pointer value permits dereferencing.

    Second case. *p is an lvalue, an object that (apart from constness) can
    appear as the Left hand side of an assignment. &*p is an rvalue.

    Hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 28, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page