tags:

views:

416

answers:

9

This might be a dumb question to some of you and maybe I asked this question wrong, because I am new to c++. But I notice when working in a lot of win32 applications, you use a lot of resources that are pointers. Why do you have to always acquire a objects pointer ? why not initiate a new instance of the class. and speaking of that, I notice in most cases you never initiate new object's, but always call on methods that return that pointer. What if that pointer is being used somewhere else. couldn't you mess something up if you alter that pointer and it is being used somewhere else.

A: 

Probably because the Win32 API is "older" than mainstream object-oriented programming, it's not a C++ API at its core.

unwind
+4  A: 

Win32 was designed to work with the C language not C++.
That's why you will see return types of the defined BOOL instead of bool for example.
bool is specific to C++ and doesn't exist in C.

For Microsoft's object oriented wrapper of Win32 see MFC.

A newer framework from Microsoft since then is the .Net Framework.
The .Net framework is based on managed code though and does not run natively. The most modern way to do GUI programming on Windows is WPF or even Silverlight.

The most modern way to do unmanaged GUI programming is still using MFC, although some people still prefer using straight Win32.

Note working with pointers is not specific to C, it is still very common in C++.

Brian R. Bondy
MFC is way too old and badly designed, I wouldn't recommend using it...
dpimka
@dpimka: The poster is asking specifically about Win32. MFC is not far from Win32 and worth mentioning in that context.
Brian R. Bondy
MFC is still the _official_ OO way to do Windows in C++ though, isn't it?
John
True today, but for 1995 it was the best you could do - before RTTI, before signals, before the STL.
Martin Beckett
@John, yes today there isn't really a good way to do a C++ gui on windows. You either use winforms/WPF with managed C++ or something like Qt/wx.
Martin Beckett
MFC also has great integration with Visual studio through the resource editor.
John
@John: The recommended way is WPF now days. But that is managed code. MFC is still the latest unmanaged way.
Brian R. Bondy
@John: WinForms and WPF have ever better integration. For example you can create a tab control and drag items onto each tab and change the tab.
Brian R. Bondy
@John/@Brian: well, there is the WTL (Windows Template Library). It was never officially supported, but is still being maintained over at SourceForge (http://sourceforge.net/projects/wtl/).
Pontus Gagge
@Pontus Gagge: ya have used it a few times in the past as well.
Brian R. Bondy
+3  A: 

First reason is because passing pointers around is cheap. Pointers are 4 bytes on x86 and 8 bytes on x64. While the structure or class it points to can occupy a whole lot more in memory. So instantiating a class means reserving new memory again and again. This isn't efficient from speed and memory consumption POVs.

Another way is to pass references to objects or smart pointers or similar constructs. But win32 api was designed in the C era, so this is where it is up to now ;)

As about potential messing up with pointers - it's possible of course. But most of the time their lifetime is explicitly stated in the API (if not obvious).

dpimka
A: 

To understand the pointers you might want to read the CPlusPlus.com tutorial on pointers.

m_pGladiator
I understand pointers. I just didnt understand why C used so many of them. If anything, resources on understanding WIN32 system programs would help
numerical25
A: 

Its almost like you should try one of the many OO wrappers. Like MFC or .net.

rerun
.Net isn't a Win32 wrapper.
John
What can .net do that is not in the lower level API
rerun
@rerun: I'm a C++ developer mainly, but even I can admit that .Net enables undeniably faster development and less bug prone code. Not to mention less raw code to maintain.
Brian R. Bondy
@Brian I totally agree and mostly develop in .net but it is at its foundation built upon win32. Except in the Case of mono but I think that a little out of the discussion.
rerun
Ya it probably uses Win32 at some level, but I wouldn't say it is a wrapper. +1 anyway.
Brian R. Bondy
A: 

The Windows API is plain old C, hence the use of pointers everywhere. Also, the reason you ask Windows for a new pointer is because Windows needs to keep track of all objects... it allocates things and tells you a pointer (or sometimes just a numeric ID) to let you work with them.

John
+5  A: 

Because Win32 Api are written on plain C, not C++. So any program on almost any language can make call to those API.

Plus, there are not simple mechanism to use objects across diferent modules, and diferent languages. I.e. you can't export C++ class to python. Of course, there are technoligies like OLE/COM, but they still written on plain C. And they are bit comlicated to use.

At other hand - calls to plain C functions are standardized. So you can call routines from DLL or static lib in any language.

werewindle
A: 
  • Having C functions as the API allows both C and C++ programmers use it.
  • Windows APIs goes way back - C was famous during those days.

All the HWND, HANDLE, HDC are but a weak attempt to make clamped, objects-like data types (using struct). C FAQ has a question on this -> http://c-faq.com/struct/oop.html.

legends2k
Having C functions allows most languages, including Python, Haskell and C#, to call Win32 too. :)
Marcus Lindblom
+1 for mentioning about the other langs too, C has that genericity when it comes to ABI :)
legends2k
+17  A: 

Windows APIs were designed for C, which was and still is the most used language for system programming; C APIs are the de-facto standard for system APIs, and for this almost all other languages had and have some way to call external C functions, so writing a C API helps to be compatible with other languages.

C APIs need just a simple ABI, that consists of almost just the definition for the calling convention to use for functions (and something about the structures layout). C++ and other object oriented languages, on the contrary, require a complex ABI, that must define how objects are laid out in memory, how to handle inheritance, how to lay out the vtable, how to propagate exceptions, where to put RTTI data, ... Moreover not all the languages are object oriented, and using APIs thought for C++ with other non-object oriented languages may be a real pain (if you ever used COM from C you know what I mean).

As an aside, when Windows was initially designed C++ wasn't so widespread on PCs, and also C wasn't used so much: actually, a large part of Windows 3.11 and many applications were still written in assembly, since the memory and CPU constraints at the era were very tight; compilers were also less smart than now are, especially C++ ones. On machines where hand-forged assembly was often the only solution, the C++ overhead was really unacceptable.

For the pointers thing: Windows APIs use almost always handles, i.e. opaque pointers, to be able to change the underlying nature of every resource without affecting the existing applications and to stop applications to mess around with internal structures. It doesn't matter if the structure used by the window manager to represent a window internally is changed: all the applications use simply an HWND, which is always of the size of a pointer. You may think at this as some kind of PIMPL idiom.

However, Windows is in some way object-oriented (see for example the whole "window class" concept, or, at a deeper level, the inner working of the NT kernel, which is heavily based on the "object" concept), however its most basic APIs, being simple C functions, somehow hide this OO nature. The shell, on the other side, being designed many years after, is written mostly in C++ and it provides a really object-oriented COM interface.

Interestingly, you can see in COM all the tradeoffs that you must face in building a cross-language but still C++ biased object-oriented interface: the result is quite complicated, in some respects ugly and not really simple to use from any language. The Windows APIs, instead, being simple functions are generally more easily callable.

If you are interested in a system based on C++ APIs you may have a look at Haiku; personally, this is one of the aspects because of which I am quite interested in that project.

By the way, if you are going to do Win32 programming just with the APIs you'd better get a good book to get used to these "particularities" and to other Win32 idioms. Two well-known ones are the Rector-Newcomer and the Petzhold.

Matteo Italia
thanks. from all the responses. it makes a lot more sense now.
numerical25