 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alf P. Steinbach Guest
|
Posted: Mon Nov 21, 2005 5:51 pm Post subject: Pointers and polymorphism explained [beta preview, PDF, part |
|
|
[This is a separate posting to clc++, because clc++ has such long
response time and generally not the same participants as in
alt.comp.lang.learn.c-c++,comp.lang.c++,no.it.programmering.c++].
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. Or see the earlier
discussion* for a partial table of contents.
*) Earlier versions discussed in <url: http://tinyurl.com/e44mp>, full
<url:
http://groups.google.no/group/alt.comp.lang.learn.c-c++/browse_frm/thread/f3a096decfa9b96b/231115cb00c83eba#231115cb00c83eba>
--
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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Sat Dec 03, 2005 11:38 am Post subject: Re: Pointers and polymorphism explained [beta preview, PDF, |
|
|
"Alf P. Steinbach" <alfps (AT) start (DOT) no> wrote
| Quote: | [This is a separate posting to clc++, because clc++ has such long
response time and generally not the same participants as in
alt.comp.lang.learn.c-c++,comp.lang.c++,no.it.programmering.c++].
Again I ask for comments, corrections, praise, damnations, nits, etc.!
|
Hi Alf,
I'm enjoying reading this. As a newbie I find it quite easy to
read, however there is one thing. Perhaps it's only myself, but I find that
using 'o' as a variable causes me to have to look very carefully sometimes
to be sure it isn't actualy a zero.
More experienced readers would probably instantly recognize the context, but
I have to look carefully.
Vince Morgan
Remove UNSPAM
[email]vinhar (AT) UNSPAMoptusnet (DOT) com.au[/email]>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Dec 04, 2005 1:23 pm Post subject: Re: Pointers and polymorphism explained [beta preview, PDF, |
|
|
* Vince Morgan:
| Quote: | * Alf P. Steinbach:
Again I ask for comments, corrections, praise, damnations, nits, etc.!
Hi Alf,
I'm enjoying reading this. As a newbie I find it quite easy to
read, however there is one thing. Perhaps it's only myself, but I find that
using 'o' as a variable causes me to have to look very carefully sometimes
to be sure it isn't actualy a zero.
|
I've now fixed that, using 'object' instead, in the initial sections,
and provided an explanation where the short form is really needed to
keep tables and code from overflowing, in the section on const
correctness. The new version, hopefully a "release" version, will be
made available when it's finished. One major readability-oriented
change is a summary at the start of each section, instead of all
summaries assembled at the end, and one major addition to contents is a
more streamlined and complete discussion of the shortcomings of
std::auto_ptr -- I actually learned something myself there, because in
practice one tends to avoid using the murky areas of the language.
Uppercase I, lowercase l (ell) and uppercase O are the usual
easy-to-misread variable names*.
I didn't think lowercase o would be a problem, but, one learns!
Thanks for the feedback,
- Alf
*) I once had a mechanical typewriter where there was no digit 1, one
had to use lowercase l instead...
--
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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Mon Dec 05, 2005 10:36 am Post subject: Re: Pointers and polymorphism explained [beta preview, PDF, |
|
|
Alf P. Steinbach wrote:
[...]
| Quote: | Uppercase I, lowercase l (ell) and uppercase O are the usual
easy-to-misread variable names*.
|
Don't forget the digits 1 and 0, which can be used for anything
other than the first letter of a variable, e.g. I0l, IO1, etc.
Lowercase l an also be used to force an integral constant to
long, something like 0x10l (a long with value 16, as opposed to
0x101, an int with value 257). And with the right font, an
underscore may completely disappear.
If you're writing a book or an article, you want to be
particularly careful about the fonts you chose -- in fact, I
image most publishers have standards concerning this, and will
avoid fonts where the distinctions aren't clear. Since you're
writing a Web page, however, this is out of your hands. And of
course, even in fonts where the differences show up well on a
printer, they may not show up as well with the lower resolution
of the screen.
(This is vaguely related to the thread about proportional vs.
fixed width fonts. In theory, proportional fonts have an
advantage here, because they have one more variable which can be
used to distinguish -- and in almost all cases, a lower case l
is in fact narrower than a digit 1. In practice, however, there
are a number of proportional fonts which don't seem to
distinguish as well as they could.)
| Quote: | I didn't think lowercase o would be a problem, but, one learns!
|
It doubtlessly depends on the font being used. I can remember a
quiz concerning template metaprogramming where extensive use was
made of the names t and f (for true and false, obviously).
Normally, I wouldn't have thought those two confusing, but in
the font being used, I had considerable difficulty
distinguishing them.
| Quote: | *) I once had a mechanical typewriter where there was no digit
1, one had to use lowercase l instead...
|
Typewriters without a 0 were frequent, I think; you were
expected to use a capital O.
Of course, back in those days, we generally wrote our programs
out by hand, and sent them off to be keypunched. Lower case l
wasn't a problem, because there was no lower case; either upper
case O or the digit 0 (which one depended on the local
conventions) would get a bar through it, to distinguish it from
the other. And real programming languages ignored white space
completely, since the keypunch operator could never really get
it write, typing what was for her a bunch of gibberish. (Thus,
for example, the string "DO 10 I = 1.10" was three tokens,
"DO10I", "=" and "1.10", where as "DO10I = 1,10" was seven,
"DO", "10", "I", "=", "1", "," and "10".)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Tue Dec 06, 2005 10:30 am Post subject: Re: Pointers and polymorphism explained [beta preview, PDF, |
|
|
"Alf P. Steinbach" <alfps (AT) start (DOT) no> wrote
| Quote: | [This is a separate posting to clc++, because clc++ has such long
response time and generally not the same participants as in
alt.comp.lang.learn.c-c++,comp.lang.c++,no.it.programmering.c++].
Again I ask for comments, corrections, praise, damnations, nits, etc.!
1.1.7 |
page 14.
<quote>
The same kind of safety considerations - protect against simple mishaps -
are the basis of pointer const correctness, what you're allowed to do, and
what you're not allowed to do.
</quote>
I believe that "protect against simple mishaps" should read either "to
protect against mishaps", or, "protection against simple mishaps"
Vince Morgan
Remove UNSPAM
[email]vinhar (AT) UNSPAMoptusnet (DOT) com.au[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Tue Dec 06, 2005 10:32 am Post subject: Re: Pointers and polymorphism explained [beta preview, PDF, |
|
|
"Alf P. Steinbach" <alfps (AT) start (DOT) no> wrote
| Quote: | [This is a separate posting to clc++, because clc++ has such long
response time and generally not the same participants as in
alt.comp.lang.learn.c-c++,comp.lang.c++,no.it.programmering.c++].
Again I ask for comments, corrections, praise, damnations, nits, etc.!
1.1.7 |
page 15.
<quote>
Here you obtain a pointer to const, where the pointed to object can actually
be changing, no
matter how much you're pointing at it with your pointer to const. The
pointed to object can be
changing because it's directly accessible as o, as well as accessible via
*p. C++ allows this
because a pointer to const does not guarantee that the object is necessarily
unchanging,
</quote>
I'm not quite sure that you meant 'changing' here, as opposed to 'changed',
or 'changeable'. From a newbie point of view 'can actually be changing'
invokes the notion of a possible immediate circumstance as opposed to a
possible future event.
I started programming about 8 or so years ago in higher level languages and
scripting. I've never dealt with volotile data, or multithreaded apps, and
I think that has caused a certain pattern of thought to devellop when
writing code. The pattern is almost purely incremental.
I imagine many newbies would have this same pattern of thought, even without
having programmed in less challenging languages. So, when I read 'changing'
it hit me like like a bolt out of a blue sky.
If you actualy meant 'changing' in the sense of volotile data then I do
beleive a short comment inserted directly after 'can be changing' could
instantly clear this up.
Enjoying the read very much,
Thank you
Vince Morgan
Remove UNSPAM
[email]vinhar (AT) UNSPAMoptusnet (DOT) com.au[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Dec 07, 2005 1:59 pm Post subject: Re: Pointers and polymorphism explained [beta preview, PDF, |
|
|
* Vince Morgan:
| Quote: | 1.1.7
page 15.
quote
Here you obtain a pointer to const, where the pointed to object can actually
be changing, no
matter how much you're pointing at it with your pointer to const. The
pointed to object can be
changing because it's directly accessible as o, as well as accessible via
*p. C++ allows this
because a pointer to const does not guarantee that the object is necessarily
unchanging,
/quote
I'm not quite sure that you meant 'changing' here, as opposed to 'changed',
or 'changeable'.
|
The latter, 'is changeable'.
I meant 'can actually be changed during the lifetime of the pointer'.
Not a still picture but a dynamic movie of code execution.
| Quote: | From a newbie point of view 'can actually be changing'
invokes the notion of a possible immediate circumstance as opposed to a
possible future event.
|
Uh, well...
| Quote: | I started programming about 8 or so years ago in higher level languages and
scripting. I've never dealt with volotile data, or multithreaded apps, and
I think that has caused a certain pattern of thought to devellop when
writing code. The pattern is almost purely incremental.
I imagine many newbies would have this same pattern of thought, even without
having programmed in less challenging languages. So, when I read 'changing'
it hit me like like a bolt out of a blue sky.
|
That can explain a number of things that has surprised me in other
people's code.
The best I can come up with, at the spur of the moment (!), to
illustrate the usefulness of a more dynamic point view, is the
equivalence of the loops
a();
while( !condition )
{
b();
a();
}
// Apparently b(), then a().
and
for( ;; )
{
a();
if( !condition ) { break }
b();
}
// Apparently a(), then b().
Here I'm _guessing_ that a purely incremental, sort of "static" very
local point of view might make it diffult to see the equivalence.
| Quote: | From my usual point of view it is, however, in both cases a sequence of
actions a, b, a, b, a, b, ..., and so on, with a check of the |
termination condition after each a.
| Quote: | If you actualy meant 'changing' in the sense of volotile data then I do
beleive a short comment inserted directly after 'can be changing' could
instantly clear this up.
|
I'll try to think of something... ;-)
Thanks,
- 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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Wed Dec 07, 2005 2:46 pm Post subject: Re: Pointers and polymorphism explained [beta preview, PDF, |
|
|
"Vince Morgan" <vinhar (AT) UNSPAMoptusnet (DOT) com.au> wrote
| Quote: | quote
The same kind of safety considerations - protect against simple mishaps -
are the basis of pointer const correctness, what you're allowed to do, and
what you're not allowed to do.
/quote
I believe that "protect against simple mishaps" should read either "to
protect against mishaps", or, "protection against simple mishaps"
|
Sorry, I meant "to prtotect against simple mishaps" above.
Vince Morgan
Remove UNSPAM
[email]vinhar (AT) UNSPAMoptusnet (DOT) com.au[/email]>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Dec 08, 2005 11:58 am Post subject: Re: Pointers and polymorphism explained [beta preview, PDF, |
|
|
* Alf P. Steinbach:
| Quote: |
The new version, hopefully a "release" version, will be
made available when it's finished.
|
Not yet perfect, but:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip
To access the table of contents, use the "Bookmarks" tab in Adobe
Acrobat.
1 Pointers.
1.1 Introduction to the basics.
1.1.1 How to obtain a pointer to a given object, and how to use that pointer.
1.1.2 The nullpointer value, valid and invalid pointers.
1.1.3 How to implement out-arguments by using pointers.
1.1.4 How to implement in-arguments by using pointers.
1.1.5 How to use C++ style references instead of C style pointers for arguments.
1.1.6 How to access main arguments.
1.1.7 Const-correctness for pointers and references.
1.2 Run-time polymorphism.
1.2.1 Polymorphism.
1.2.2 The concepts of data representations and linked data structures.
1.2.3 C-style polymorphism with simulated dynamic types (just one actual type).
1.2.4 Basic use of dynamic memory allocation & deallocation.
1.2.5 How to use function pointers to simulate dynamic types, C-style.
1.2.6 Using real types and inheritance for logical sub-types, mostly C-style.
1.2.7 Using vtables, mostly C-style (also a few words about abstract classes, etc.).
1.2.8 C++ virtual member functions.
1.2.9 C++ destructors and polymorphic delete.
1.3 Safety for dynamically allocated objects.
1.3.1 Object ownership and the C++ std::auto_ptr.
1.3.2 Exception safety for new: RAII and C++ constructors.
1.3.3 Ensure dynamic allocation by using C++ access specifiers & friendship.
1.3.4 An aside on wrapping boost::shared_ptr (a.k.a. std::tr1::shared_ptr).
1.3.5 Ensure that smart pointers are used, by overloading operator new.
1.3.6 Combat slicing by removing access to copy assignment.
1.3.7 Value copying problems & possible solutions.
1.3.8 Cloning as a solution to ownership and aliasing problems.
1.3.9 Intentional sharing as a solution to ownership and aliasing problems.
1.4 An introduction to exceptions.
1.4.1 How to generate and handle exceptions.
1.4.2 Exception usage example: conversion between numeric values and text.
1.4.3 Exceptions and pointers, including the concept of "hard" exceptions.
1.5 Basic serialization and de-serialization.
1.5.1 A serialization format for hierarchical data structures.
1.5.2 De-serialization: a simple non-OO recursive descent parser.
1.5.3 Serialization: a simple generator using intrusive OO code.
1.5.4 A generator using non-intrusive non-OO code (introducing C++ RTTI).
1.5.5 A generator using non-intrusive OO code (introducing the visitor pattern).
1.6 Notes on C++ inheritance as it pertains to pointers.
1.6.1 Passing Base/Derived-class pointers by reference.
1.6.2 The Liskov substitution principle (more about mutable/immutable).
1.6.3 Covariance, contravariance and invariance.
1.7 Closing words & acknowledgements.
--
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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|