New C++ Tutorial

6 views
Skip to first unread message

Will Fitzgerald

unread,
Aug 15, 2003, 5:48:04 PM8/15/03
to
Hi,

I've created a short C++ tutorial, aimed at people who already have
experience with an object-oriented programming language (like Java or
Python).

It's at: http://www.entish.org/realquickcpp/

I'd be glad to hear of any feedback.

Will Fitzgerald

Gianni Mariani

unread,
Aug 15, 2003, 6:24:29 PM8/15/03
to

I had a quick skim.

Look good !


Thanks
G

Default User

unread,
Aug 15, 2003, 6:57:50 PM8/15/03
to

I couldn't tell, none of your links work in Mozilla or NS. With a
simplistic page like that, having such poor markup is a very poor idea.
It's just as easy to get it right.


Brian Rodenborn

Kevin Goodsell

unread,
Aug 15, 2003, 8:08:43 PM8/15/03
to

After a quick look I only found a few errors.

[quote]
When the #included file is given as a string, the definitions are looked
for locally (usually in the same directory)
[/quote]

It is *not* a string. It just happens to look similar to a string
literal. They are quite distinct.

[quote]
#ifndef _UnigramTextClassifier_H_
#define _UnigramTextClassifier_H_
[/quote]

_UnigramTextClassifier_H_ is a reserved identifier. In general, I
recommend never beginning any identifier with an underscore, since such
identifiers are reserved in many contexts and the exact rules are
complicated.

Also, it uses the term "method" where the correct term is "member
function" and uses var++ where ++var would be preferable.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Gianni Mariani

unread,
Aug 15, 2003, 9:03:49 PM8/15/03
to

That's funny, I use moz and it worked fine for me.

What did you do ?

Kevin Goodsell

unread,
Aug 15, 2003, 8:32:56 PM8/15/03
to
Default User wrote:

Worked fine for me in Mozilla Firebird.

However, I was not able to validate it:

http://validator.w3.org/check?uri=http%3A%2F%2Fwww.entish.org%2Frealquickcpp%2F

Apparently no character encoding is specified.

Christoph Rabel

unread,
Aug 16, 2003, 4:15:57 AM8/16/03
to
Gianni Mariani wrote:

> Default User wrote:
>> I couldn't tell, none of your links work in Mozilla or NS. With a
>> simplistic page like that, having such poor markup is a very poor idea.
>> It's just as easy to get it right.
>
> That's funny, I use moz and it worked fine for me.
>
> What did you do ?

e.g. on the page:

http://www.entish.org/realquickcpp/memorymgt.html

The link

http://www.entish.org/realquickcpp/classesetall.html

doesnt work at all.

cya

Christoph

Kevin Goodsell

unread,
Aug 16, 2003, 2:45:55 PM8/16/03
to
Christoph Rabel wrote:
>
> e.g. on the page:
>
> http://www.entish.org/realquickcpp/memorymgt.html
>
> The link
>
> http://www.entish.org/realquickcpp/classesetall.html
>
> doesnt work at all.
>

It "works" for me in the sense that it attempts to load the page. But it
404s.

Christoph Rabel

unread,
Aug 16, 2003, 5:17:22 PM8/16/03
to
Kevin Goodsell wrote:
> It "works" for me in the sense that it attempts to load the page. But it
> 404s.

So, for you links that 404 work for you?
Please dont answer its a rhetorical question...

Christoph

Alf P. Steinbach

unread,
Aug 16, 2003, 5:36:37 PM8/16/03
to

I just skimmed it and didn't see any obvious disinformation. Also, nice
readable layout. At least in Internet Explorer.

Incorrect:

* Macro symbols such as '_UnigramTextClassifier_H_' are reserved by
the implementation, and gives undefined behavior.

* You cannot put new things into namespace std, as you do with class
UnigramTextClassifier. The compiler may incorrectly allow it, but
that does not mean another compiler will.

Details:

* Why write "i++" when you can write "++i"?
Bad habit to learn.

* Why use _leading_ underscores in member names?
Bad habit to learn, especially since leading underscore followed by
uppercase letter (not that I saw one in your code, except for the
header guards) is reserved for the implementation.

* It's a good idea to put 'const' on methods that do not change the
observable state of an object.
Bad habit to learn not to do that.

Hth.,

- Alf

Pete Becker

unread,
Aug 16, 2003, 6:16:56 PM8/16/03
to
"Alf P. Steinbach" wrote:
>
> * You cannot put new things into namespace std, as you do with class
> UnigramTextClassifier. The compiler may incorrectly allow it, but
> that does not mean another compiler will.
>

The compiler correctly allows it. <g> The behavior of a program that
adds names to std is undefined, so the standard does not require any
particular behavior when that's done. In practice this isn't diagnosed,
because compilers use ordinary files for their headers, and when you do
that you can't easily tell whether it's the user or the implementation
that's adding those names.

So the advice is right: don't do that. Compilers may correctly reject
it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Alf P. Steinbach

unread,
Aug 16, 2003, 6:41:00 PM8/16/03
to
On Sat, 16 Aug 2003 18:16:56 -0400, Pete Becker <peteb...@acm.org> wrote:

>"Alf P. Steinbach" wrote:
>>
>> * You cannot put new things into namespace std, as you do with class
>> UnigramTextClassifier. The compiler may incorrectly allow it, but
>> that does not mean another compiler will.
>>
>
>The compiler correctly allows it. <g>

I guess I earned that one... ;-)


> The behavior of a program that
>adds names to std is undefined, so the standard does not require any
>particular behavior when that's done. In practice this isn't diagnosed,
>because compilers use ordinary files for their headers, and when you do
>that you can't easily tell whether it's the user or the implementation
>that's adding those names.

I can. Some (all?) compilers don't. Simplest way would be to have a
boolean state reflecting the directory of the currently parsed code;
more sophisticated, to have all standard headers precompiled into some
kind of database; and so on.

I think the real reason why there's no diagnostic is that it would add
no real value to the compiler, while requiring some work to implement.

Pete Becker

unread,
Aug 16, 2003, 7:01:36 PM8/16/03
to
"Alf P. Steinbach" wrote:
>
> On Sat, 16 Aug 2003 18:16:56 -0400, Pete Becker <peteb...@acm.org> wrote:
>
> > The behavior of a program that
> >adds names to std is undefined, so the standard does not require any
> >particular behavior when that's done. In practice this isn't diagnosed,
> >because compilers use ordinary files for their headers, and when you do
> >that you can't easily tell whether it's the user or the implementation
> >that's adding those names.
>
> I can. Some (all?) compilers don't. Simplest way would be to have a
> boolean state reflecting the directory of the currently parsed code;
> more sophisticated, to have all standard headers precompiled into some
> kind of database; and so on.

But if anyone did that they'd get complaints that they were locking in
their own standard headers.

>
> I think the real reason why there's no diagnostic is that it would add
> no real value to the compiler, while requiring some work to implement.
>

Well, "some work" is in the eye of the beholder. It's far from trivial,
and, as you say, it adds no real value.

Alf P. Steinbach

unread,
Aug 16, 2003, 7:15:32 PM8/16/03
to
On Sat, 16 Aug 2003 19:01:36 -0400, Pete Becker <peteb...@acm.org> wrote:

>"Alf P. Steinbach" wrote:
>>
>> On Sat, 16 Aug 2003 18:16:56 -0400, Pete Becker <peteb...@acm.org> wrote:
>>
>> > The behavior of a program that
>> >adds names to std is undefined, so the standard does not require any
>> >particular behavior when that's done. In practice this isn't diagnosed,
>> >because compilers use ordinary files for their headers, and when you do
>> >that you can't easily tell whether it's the user or the implementation
>> >that's adding those names.
>>
>> I can. Some (all?) compilers don't. Simplest way would be to have a
>> boolean state reflecting the directory of the currently parsed code;
>> more sophisticated, to have all standard headers precompiled into some
>> kind of database; and so on.
>
>But if anyone did that they'd get complaints that they were locking in
>their own standard headers.

Directory check would be easy to make configurable, and for any usable
compiler would have to be configurable anyway (otherwise the compiler's
headers would always have to be installed in the same place); for the
idea of precompiled standard headers, simply offer the precompilation
tool to the compiler user; and so on.

Pete Becker

unread,
Aug 16, 2003, 7:20:20 PM8/16/03
to
"Alf P. Steinbach" wrote:
>
> Directory check would be easy to make configurable, and for any usable
> compiler would have to be configurable anyway (otherwise the compiler's
> headers would always have to be installed in the same place); for the
> idea of precompiled standard headers, simply offer the precompilation
> tool to the compiler user; and so on.
>

Yup. Not easy.

Dave O'Hearn

unread,
Aug 16, 2003, 9:05:04 PM8/16/03
to
Christoph Rabel <od...@hal9000.vc-graz.ac.at> wrote:
> Gianni Mariani wrote:
> > Default User wrote:
> >> I couldn't tell, none of your links work in Mozilla or NS. With a
> >> simplistic page like that, having such poor markup is a very poor idea.
> >> It's just as easy to get it right.
> >
> > That's funny, I use moz and it worked fine for me.
> >
> > What did you do ?
> [...]

That gives a 404 error. It isn't a browser issue.

--
Dave O'Hearn

Buster Copley

unread,
Aug 18, 2003, 11:55:48 AM8/18/03
to
Will Fitzgerald wrote:

I'm not very impressed with the section on memory management. The
technique of pairing every 'new' with a 'delete' leads to memory
leaks if an exception is thrown between the two statements. A much
better way is use automatic variables whose destructors deallocate
resources. Sometimes std::auto_ptr is appropriate; otherwise you
can use the smart pointers provided by the boost library or write
'handle' classes specific to the kind of resource in question.

This technique, known as RAII (resource acquisition is initialization)
is an extremely important part of C++ programming. It works so well,
and makes resource management so much more predictable, that garbage
collection has become a dirty word (ok, two words) in some C++ circles.

Buster

Will, I didn't intend to mail this to you. I meant to post it to the
newsgroup. Sorry.

Will Fitzgerald

unread,
Aug 19, 2003, 6:44:20 PM8/19/03
to
Thanks to all for the useful feedback. I've made some changes to the
tutorial as a result. (A few more are still necessary).

-- Will

Reply all
Reply to author
Forward
0 new messages