Advertisement

Why is C++ so confusing?

Started by February 20, 2002 11:46 PM
11 comments, last by Tac-Tics 22 years, 10 months ago
quote: Original post by Kippesoep
LPCSTR is a "Linear Pointer to a Constant STRing", so that''s "const char *" to you.


I think it should be "Long Pointer to a Constant STRing". The naming happen way back in Win16 era, where there are near and far pointer. And the "L" means far (or long) pointer... and the "L" still surviving until Win32 (now).

"after many years of singularity, i'm still searching on the event horizon"

Looks like you find the Win32 API confusing (like pretty much everyone else), not so much C++ itself.

quote:
In windows programming, why do they use all those abstract class names like HWND, LPCTSTR, and WNDCLASSEX? I don't get it. Anyway, is there any documentation on what each of those classes actually are? I know a lot of times it's more helpful to learn "hands-on", finding out what each expression does as you progress.... but it'd be nice to know why so many of these objects are set to NULL in creating a window. If not formal documentation, is there any place that explains the basic "jist" of all of those classes?


The Win32 API is pure C, so there aren't any classes involved. You're actually asking about 3 different things: a handle, a typedef and a struct.

Don't get confused by the name of WNDCLASSEX, it's a simple struct that describes a custom 'window class'. A 'window class' has nothing to do with classes like you know them from Java. It's just used to distinguish different 'types' of windows: buttons, editboxes, popupwindows,... Hope that makes sense.

HWND stands for Handle-to-WiNDow.

The Win32 API tries to hide implementation details from you, it stores most information internally and gives you 'handles' to operate on. A handle is a lot like an object reference in Java. You can use functions to operate on the 'object', but you can't access it's hidden internal data. Like OO in a non-OO language.

  HWND window = CreateWindow(...);ShowWindow(window, SW_SHOWDEFAULT);  


is similar to:

  Wnd window = new Wnd(...);window.show(SW_SHOWDEFAULT);  


You have to clean up some handle-types yourself (HWND isn't one of them), so look this up in the docs.

Typedefs are used to create aliases for typenames. LPCTSTR is short for Long-Pointer-to-Constant-STRing. Don't worry about about the long-pointer, that's just something of days long gone.

The T in the middle of LPCTSTR means that the type can be used for both ASCII (1 byte chars) and UNICODE (2 byte chars) strings. UNICODE is needed for languages that use a lot of different characters (more than 256). LPCTSTR is a simple typedef for either LPCSTR or LPCWSTR , in turn these stand for 'const char*' and 'const wchar_t*' respectively. If UNICODE is defined, all LPCTSTRs willbecome const wchar_t*'s, otherwise they'll turn into ordinary const char*'s.

you'll find something like this in the windows headers:

  #ifdef UNICODEtypedef const wchar_t* LPCTSTR;#elsetypedef const char* LPCTSTR;#endif  


Java uses 2 bytes for chars, so it doesn't have this problem.

You can often pass in NULL (or 0) if you want a function to use the default value for that parameter. In C++ NULL is really 0, so it doesn't matter what you use. Some people use NULL for a null-pointer or a null-handle and 0 for numbers only. Personally I use 0 for everything.

If there is documentation you will most likely find it at msdn.microsoft.com/library. Under the 'windows development/Win32 API/SDK documentation' node.


quote:
Also, I keep hearing the word library being flashed around everywhere. Are there any good tutorials out there on how libraries work in C++? As I understand it, libraries just provide more classes and functions for you to use, but how are they different from *.h files? What are DLL's (and why did they destroy my old computer)?


In the broadest sense, 'library' just means a bunch of ready-to-use functions or classes. Java uses libraries too, for example: Swing is a GUI library. C++ only has a minimal (but powerful) standard library, whereas in Java the standard library is huge. C++ relies on non-standard libraries for all but the most basic things. The advantage of Java is that you can easily port apps, because all classes exist for every Java platform. The advantage of C++ is that platform-specific libraries are usually more flexible. Unlike in Java, C++ code is usually seperated in different files for declarations and definitions. Headerfiles mostly contain declarations, they tell you, and the compiler, what can be accessed in the corresponding sourcefiles, in the library code (this thread might help).

In a more narrow sense, a library can also mean a bunch of ready-to-use compiled or assembled code. Usually only the sourcefiles contain the actual code, so they get compiled, the header files, again, tell you what functions and classes are in the library.

On some platforms, like Windows, you have 2 different types of (narrow sense) libraries: statically linked libraries and dynamically linked libraries, like DLLs on Windows. Statically linking with a library means that the compiled code in that library is combined with your own code to produce a single executable, at compile-time. Of course if a lot of programs use the same library that would be a complete waste of space. Dynamically linking solves this. The library's executable code is kept in a seperate file, a DLL, and applications can load and unload that library and use it's code at run-time. This can be little bit harder for the application to handle and you must make sure that the correct DLLs are present when you run the app, but it saves space, and the DLL can be replaced and updated seperatly. Things like DirectX make you link with a static libraries, and the static libraries handle the actual DirectX DLLs for you.

If you mess up DLLs that Windows itself uses, that's the same thing as messing up a piece of Windows executable code, so it's not very surprising that things don't work properly anymore.

The file-format of the libraries differs between platforms (meaning OSs but also compilers). That's why static libraries compiled for Visual C++ won't work out-of-the-box for your Borland compiler.

quote:
After that, how do I start working with DirectX. I understand it's a general purpose API for graphics, sound, and input. How do I get it and how to I use it in my programs? I have a book on it, but it works with Visual C++ and mentions libraries several times (thus explaining my previous question). I just need to know how to get started with it.


Most people download the DirectX SDK from Microsoft's site, it contains all libraries, examples, docs, ... But like I said, the libraries won't work for Borland. You have to convert them somehow (implib.exe or something). I don't use the Borland compiler so I can't help, but I'm sure someone else can. (There's a Borland-specific forum at GameDev called 'turbo')

Edited by - kvh on February 21, 2002 1:01:54 PM
Advertisement
Thanks guys for all your help (maybe it was too much help? =-)
Thanks again.


"I''''m not evil... I just use the power of good in evil ways."

This topic is closed to new replies.

Advertisement