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

A

Alf P. Steinbach

* Alf P. Steinbach:

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:
 
A

Alf P. Steinbach

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

Vince Morgan

Alf P. Steinbach said:
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!
 
V

Vince Morgan

Alf P. Steinbach said:
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
(e-mail address removed)
 
A

Alf P. Steinbach

* Vince Morgan:
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
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top