Archived

This topic is now archived and is closed to further replies.

Why is C++ so confusing?

This topic is 5777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I really understand Java easily. But there''s something about learning the more complex parts of C++ I don''t get. I''m trying to figure out Windows programming and graphics and stuff, but I''m having a much more difficult time doing so. So here are my questions.... 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? 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)? And I how do I link libraries to my programs? Keep in mind I have Borland''s C++ compiler, so I can''t just go to some fancy menu and click "add library" =-) 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. Any help would be *extremely* appreciated. In fact, I may construct a shrine out of soap in your honor if your respond =-) I really want to start working with C++ as well as with Java, but I need some help. "I''''m not evil... I just use the power of good in evil ways."

Share this post


Link to post
Share on other sites
Those structs are made by the windows.h file...
a HWND is a - Handle to a WiNDow (Yeah its cryptic I agree)
WNDCLASSEX is an extended window class (for making windows which
are up to date
and LPCSTR is a Long pointer to a string of some sort

If you know Hungarian notation things are a little easier but
not that much heh...

a lib is basically a compiled .h file you can use.

And as far as the other questions well you need many books hehe

-=Lohrno

Share this post


Link to post
Share on other sites
First off HWND LPCTSTR are not classes and WNDCLASSEX is a struct.

HWND is a four byte value that acts as an ID for a window you''re working with. LPCTSTR is a double pointer to a character (don''t quote me on that one). And WNDCLASSEX is just a set of parameters for your window. If you don''t have MSDN and plan on seriously doing development under windows then I suggest getting it.

You do understandt he concept of using .h and .cpp files right? .h Files are for prototypes for functions/classes and the .cpp files are where the function is implemented. Just remember as a starting c++ coder that you can not allocate any memory within a .h file. Just tell the compiler how the memory looks (if that doesn''t make any sense then sorry).

Ok libraries are precompiled .cpp files. This is so you can distribute code without revieling source. you use the .h files just like you would a regular one. DLLS are dynamicaly linked. Instead of compiling the .lib files into your project you use a precompiled .dll file and the you call functions within the dll. For more info on this topic do a search on this site or google. .lib files are similar to .class files (isn''t that how you distribute code in java?). I''m trying to reference Java stuff for you but I don''t know very much java, just the basics of it. To link a lib in your project you need to put it in your make file if you''re using a command line compiler. For information on make files do a search on google.

If you want to start working with DX I suggest you study as much code as you can. There is a site deticated to it at nexe.gamedev.net (link on main page). In the DX sdk there are help files along with samples. The code is very messy, but once you get the jist of it you can implement your own stuff. Also I would like to point out that using either openGL or dx you need to have a firm knowledge in 3d basics. I also suggest that if you''re going to use DX to know pointers like the back of your hand because DX uses COM which relies heavily on double pointers etc.

Good Luck,
Give me an email if you need any more help.
-ecko

-----------------------------------------------------------
"People who usualy use the word pedantic usualy are pedantic!"-me

Share this post


Link to post
Share on other sites
Maybe you shouldn''t try to learn Win32-- you should learn C++. Example: I''ve been coding in C++ for over a year now, and I still don''t know Win32. I know some that I remember from LaMothe''s Windows book, but that is all and that is not much.

Simple as that.

As to how confusing it is, well, you get used to it.

Share this post


Link to post
Share on other sites
quote:
Original post by Tac-Tics
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?



Ahh... HWND, LPCTSTR... et cetera et cetera et cetera...

That''s just Microsoft''s fault. They use a stupid naming convention called Hungarion Notation-- which accounts for their confusing structure/object names. Don''t ask why they honestly thought it was a good idea... I don''t know.

And if you''re a beginner, use Allegro or SDL-- I refuse to suggest Directx to beginners when even many professionals don''t like using it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I know it''s scary for beginners, but it''s great when you get used to it. For an experienced programmer, it makes things easy because you can pretty much figure out the function by just looking at the prototype.

And remember, the Win32 API is written in C NOT C++.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
And remember, the Win32 API is written in C NOT C++.


If you want a C++ style of coding, use MFC. There is also OWL (the one Borland makes; not Microsoft) but they are essentially the same thing. MFC is just a wrapper of the win32 code except for some of the advanced things it ofers like document-view architecture and some of the data types they provide. I have a fair amount of experience with MFC and win32 never OWL, so I couldn''t tell you which one is more suited for coding games in.

When I do program for Windows I use the win32 API for games and MFC for regular apps. Games don''t need a large amount of interfacing with the OS, so win32 is simple and efficient for that; MFC is good for apps since it usually reduces coding time and has some fairly nifty features. I''ve been coding on Linux/FreeBSD for a while, so I haven''t messed with Windows too much lately.

I generally code in OpenGL, so I can''t help you out too much with Direct3D (unless of course you want some math to do all that 3D stuff; then I could help you; I sure am glad Mathematics isn''t Operating System specific )

Anyways, Good Luck

Share this post


Link to post
Share on other sites
MSDN!!!

that''s where you can find the official documentation from microsoft. And it''s avaiable online:

http://msdn.microsoft.com/

Also you can download the microsoft platform sdk''s from there which will contain everything you need to program for windows and also directx and some other libraries.
It contains both the headers, libs and documentation to do it.

Also it helps to sort out a couple of things in the microsoft naming conventions:

Everything that starts with H are a handle to an object (not exactly the same as pointers)
Everything that starts with LP is a pointer, sometimes only P.
Everything that has C after the type is constant.
And here''s the deal with strings:
TSTR = tchar string (unicode if unicode is defined, and multibyte if multibyte is defined.)
STR = regular string.
WSTR = wide (unicode) string.

So here''s some examples:
HWND = Handle to a window
LPCTSTR = Pointer to a Constant Tchar STRing.

Share this post


Link to post
Share on other sites
quote:
Original post by Floppy
I have a fair amount of experience with MFC and win32 never OWL, so I couldn''t tell you which one is more suited for coding games in.


Neither. Heh. Keep away from MFC as much as possible, hell, don''t even bother using it unless that''s the only way to do something (in which case, it''s probably a bad thing that you want to do anyways).

As for DirectX, heh, it''s quite overwhelming at first, even for people who''ve been programming for years. Still, unlike what was said earlier:

quote:
Original post by Some Guy
when even many professionals don''t like using it.


That really only applies to DirectX 5 and older. Nowadays, the only issues with DirectX is the lack of DirectDraw in DX 9, and the fact that the DirectX APIs are Windows only. Still, for a beginner, try SDL, says a friend of mine. I''ve never used it myself, though, so don''t hold me to his advice.


Chris ''coldacid'' Charabaruk <ccharabaruk@meldstar.com> <http://www.meldstar.com/~ccharabaruk/>
Meldstar Studios <http://www.meldstar.com/> - Creation, cubed.

This message double ROT-13 encrypted for additional security.

Share this post


Link to post
Share on other sites
LPCSTR is a "Linear Pointer to a Constant STRing", so that''s "const char *" to you. It''s shorter to type and the usage is clearer (the char pointer could be any data, not just a string).

I find all-uppercase types a bit messy to read, although you could probably improve things slightly by enabling syntax colouring for these types as though they were reserved words. Still, it''s usually quite easy to figure out what kind of parameters they are. All Windows handles start with H, and the names are pretty descriptive (HMENU, HFONT, HPEN, HDC, HMODULE, HWND, HINSTANCE). No confusion there, as long as you know what menus, fonts, pens, device contexts, modules, windows and instances are...

The Borland compiler most probably doesn''t support this, but I like the MSVC way:
#pragma comment (lib, "mylibrary.lib")
Of course, that''s not portable, but it does ensure that when you re-use code (in MSVC) you don''t have to check all the code for the necessary libraries and add those to your project link settings.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

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 UNICODE
typedef const wchar_t* LPCTSTR;
#else
typedef 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

Share this post


Link to post
Share on other sites