f



Quick question re: Petzold's "Programming Windows"

As a student of C++ (but have never really looked at much C-specific 
code), I plan to study Windows programming after getting a good hold of 
C++. I've read that Petzold's classic book uses C examples. Can someone 
with knowledge of C++ (but not necessarily C) still use this book 
effectively? I've heard that C and C++ zyntax are similar, but I'm not 
sure how similar, and if they are similar, are the similar enough to 
follow Petzold's lessons. Thanks!

Jack

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
gsfsdfssdffsd
6/28/2003 11:15:19 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

5 Replies
512 Views

Similar Articles

[PageSpeed] 34

gsfsdfssdffsd wrote:

 > As a student of C++ (but have never really looked at much C-specific
 > code), I plan to study Windows programming after getting a good hold of
 > C++. I've read that Petzold's classic book uses C examples. Can someone
 > with knowledge of C++ (but not necessarily C) still use this book
 > effectively? I've heard that C and C++ zyntax are similar, but I'm not
 > sure how similar, and if they are similar, are the similar enough to
 > follow Petzold's lessons. Thanks!

The biggest differences that you are likely to come across are:

1) C uses () to indicate an arbitrary parameter list

2) C allows you to specify the types of parameters between the
declaration line and the function body, something like this:

int foo(bar)
    int bar;
{
    return 0;
}

3) C++ requires casting to assign void* to any other pointer type, where
C does not.  Since many Win32 API functions return HANDLE, which is
often defined as void*, this could be quite a common problem.

Apart from having to work around these and other small differences
between C and C++, the basic concepts he uses in the book remain the
same regardless of what language you use.  Some of the specific code
samples may need a little massaging to work without error on a C++
compiler, but you shouldn't have to change the structure of them.

-- 
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Corey
6/29/2003 11:33:41 AM
gsfsdfssdffsd wrote:

> As a student of C++ (but have never really looked at much C-specific
> code), I plan to study Windows programming after getting a good hold of
> C++. I've read that Petzold's classic book uses C examples. Can someone
> with knowledge of C++ (but not necessarily C) still use this book
> effectively? I've heard that C and C++ zyntax are similar, but I'm not
> sure how similar, and if they are similar, are the similar enough to
> follow Petzold's lessons. Thanks!
> 
> Jack

I run into the same problem with Unix programming.  C is pretty much the
native language of Unix, and the POSIX api is implemented as a C library. 
The same issue exists on Windows, where the Win32 api is accessable as C
function calls.  This really doesn't cause to much of a problem though, as
C functions can be called directly from a C++ program.  Here are a few
things to keep in mind, though.

1)  Windows has a C++ class library called MFC (Microsoft Foundation
Classes) that acts as an object oriented wrapper around the Win32 api. 
This is probably a more comfortable starting point for someone familiar
with C++ and OOP.  The (rather thick at around 1300 pages) book I found to
learn it is "Programming Windows with MFC" by Jeff Prosise.

2)  C is basically like C++ without the following items:
        - function overloading

        - templates

        - classes.  The closest thing in C is a struct, which when using C can't
        have member functions.

        - all of the C++ features that revolve around classes(inheritance, RTTI,
        etc.)

        - strong checking of parameter types for functions.  In C, consider the
        following function prototype:

        int foo(int param); 

        if you call the function like this:

        char bar = 'b';

        int num = foo(bar);

        or like this:

        int     number;
        char    bar;

        foo(number, bar);

        you will NOT get a compiler error.  Sometimes I see this kind of thing
        when reading someone elses C code, and it confused the hell out of me when
        first encountered it.

        - the ability to declare variables anywhere in a function.  In C you MUST
        declare variables immediately after the open brace of the function.  The
        compiler will give an error if you don't.

C, escpecially for GUI programming, also has one other major issue that is
confusing to people coming from C++:  The heavy use of preprocessor macros.
  
GUI programming is one of those things that practically begs for an object
oriented proramming model.  Unfortunately, C has no built in facility for
such, and it's not uncommon to have a "hand rolled" solution built using
the preprocessor and other tools to generate the rather complex code needed
to simulate some OOP concepts.  Windows programming does this, and it is
sometimes confusing to see.  Until you get used to it, you may be a bit
confused, since a lot of the stuff that the preprocessor is being used for
is actually a part of the language in C++.  Keep this in mind whenever you
see code like:

HWNDCLASS foo;

LRESULT CALLBACK bar( HPARAM blah )
{}

all of the stuff in upper case is expanded to actual C code when run on the
code.  Luckily the fact that it's in all uppercase make is easy to
recognize.

Keep these point in mind, and you shouldn't have too much trouble adapting.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Sean
6/29/2003 12:35:21 PM
Sean Fraley <sfraley@rhiannonweb.com> wrote in message
news:<RVALa.256584$VP.51946405@twister.neo.rr.com>...
> gsfsdfssdffsd wrote:

> 1)  Windows has a C++ class library called MFC (Microsoft Foundation
> Classes) that acts as an object oriented wrapper around the Win32 api.

> This is probably a more comfortable starting point for someone
familiar
> with C++ and OOP.  The (rather thick at around 1300 pages) book I
found to
> learn it is "Programming Windows with MFC" by Jeff Prosise.

If you are truly a student of C++, and not someone who is already
fluent in C++ and taking a class only to satisfy curriculum
requirements, please keep in mind that the Windows GUI is one of the
worst possible vehicles for practicing the language. There are other
"bad" vehicles like COM.

The problem with these frameworks is that if you are not careful in
disciplining your mind while using them, your perspective of C++ will
become unduly contaminated, and you might develop the inaccurate first
impression that C++ is an "ugly" language, when in fact, it is the
specific use of it that is ugly.

Unfortunately, tens of thousands of other C++ students have suffered
this fate, and their notion of the nature of the language have been
inaccurately and adversely influenced by these models, severely
limiting their effectiveness in using the language in an
object-oriented fashion.  Many give up in frustration and switch to
other languages like Java.

My best advice is to go ahead with Petzold's book, stay away from MFC
(it's plain nasty and many Win/GUI experts fall back to plain old
C-style GUI anyway), and when you are working with the C-style GUI
controls, resist the temptation to let the controls act as consummate
repositories for your displayed data. You should maintain pure C++
objects external to the GUI controls (like map, etc.), and store
pointers to these objects in the controls so that the data can be
referenced later when it needs to be rendered.  You'll see what I mean
when you get to ListView, TreeView, etc..

If you cannot resist the temptation to try MFC, then may I suggest
that you learn with prudence, but repeatedly remind youself...

"this is not C++, this is just someone's use of it.."
"this is not C++, this is just someone's use of it.."
"this is not C++, this is just someone's use of it.."

Trying to make use of the virtuous features of C++ within the context
of the Windows GUI is like sweeping a dirt floor -  it's almost
pointless.

Caveat emptor.

-Chaud Lapin-

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
unoriginal_username
6/29/2003 10:18:34 PM

gsfsdfssdffsd schrieb:

> As a student of C++ (but have never really looked at much C-specific
> code), I plan to study Windows programming after getting a good hold
of
> C++. I've read that Petzold's classic book uses C examples. Can
someone
> with knowledge of C++ (but not necessarily C) still use this book
> effectively? I've heard that C and C++ zyntax are similar, but I'm not
> sure how similar, and if they are similar, are the similar enough to
> follow Petzold's lessons. Thanks!
> I haven't read Petzold's book, but I have to ask you a serious
question:

Do you really want to learn Windows programming, or aren't you
interested
in the more general concept of GUI/threads/network/programming?

There exist several good libraries/frameworks around that capture
platform-dependent code, and offer you a platform-INDEPENDENT API.
This way, you do not limit yourself to Windows.

Examples of such libraries are QT (www.trolltech.com), which isn't
cheap,
or as an (IMHO excellent) free library, wxWindows(www.wxwindows.org)


regards,

Thomas


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Thomas
6/30/2003 4:07:00 PM
unoriginal_username@yahoo.com (Le Chaud Lapin) writes:

[snip lots of justified anti-MFC ranting]

> Trying to make use of the virtuous features of C++ within the context
> of the Windows GUI is like sweeping a dirt floor -  it's almost
> pointless.
[snip]

Not true. One of the most virtuous features of C++ is wrapping ugly
    code to present the rest of the application with a clean
    interface. There is no reason this cannot be done for MFC, or w32
    api.

However it is better to obtain an existing wrapper (wxwindows, qt,
    etc) than write it yourself.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
llewelly
6/30/2003 9:02:52 PM
Reply: