Win32应用程序不那么面向对象的,为什么有这么多的三分球? [英] win32 application aren't so object oriented and why there are so many pointers?

查看:180
本文介绍了Win32应用程序不那么面向对象的,为什么有这么多的三分球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一些你一个愚蠢的问题,也许我问这个问题错了,因为我是新的C ++。不过,我注意到有很多的Win32应用程序时,你用了很多是指针资源。为什么你总是收购对象指针?为什么不启动类的新实例。与那说,我在大多数情况下,发现你永远不启动新的对象,但总是在返回该指针调用方法。如果正在使用该指针其他地方。不能你摆乌龙,如果你改变这种指针,它正在其他地方使用。

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.

推荐答案

的Windows API被设计为C,这是现在仍然是系统编程中最常用的语言; çAPI对于系统API的事实标准,并为此几乎所有其他语言有,并有一些方法来调用外部C函数,所以写一个C API有助于与其他语言兼容。

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.

ç的API只需要一个简单的ABI中,由几乎只是定义调用约定用于功能(和一些有关的结构布局)的。 C ++等面向对象的语言,相反,需要一个复杂的ABI,它必须定义什么对象是如何在内存布局,如何处理好继承,如何布局的虚函数表,如何传播的例外,在那里把RTTI数据, ......而且并非所有的语言都面向对象,并使用想了想C ++与其他非面向对象语言的API可能是一个真正的痛苦(如果你曾经从用于COM C,你知道我的意思)。

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).

顺便说一句,当Windows最初设计C ++就没有那么wides $ P $垫在个人电脑上,也没有用c的这样的多:实际上,大量的Windows 3.11的一部分和许多应用程序仍用汇编写的,因为在时代的内存和CPU的限制是非常严格的;编译器也不太聪明比现在,特别是C ++的。在一些机器上手工锻造组装往往是唯一的解决方案,C ++开销真的不能接受的。

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.

有关指针的事:Windows APIs的使用几乎总是手柄的,即不透明的指针,是能够改变每个资源的基本性质,而不会影响现有的应用程序,并与各地停止应用程序乱内部结构。没关系,如果由窗口管理器所用的结构重新present一个窗口内部被改变:所有的应用程序都使用简单的HWND,这是一个指针的大小始终。你可能会认为这是某种PIMPL成语。

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.

不过,Windows以某种方式(例如,见整个窗口类的概念,或者,在更深的层次,NT内核,这在很大程度上基于对象概念的内部工作面向对象),但其最基本的API,是简单的C函数,在某种程度上隐藏这个OO性质。外壳,在另一边,被设计了许多年之后,主要是用C ++编写,它提供了一个真正的面向对象的COM接口。

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.

有趣的是,你可以看到在COM一切,你必须面对在建设跨语言,但仍然C ++偏向面向对象的接口的权衡:结果是相当复杂的,在某些方面难看,并没有真正简单的从任何使用语言。本的Windows API,相反,是简单的功能一般都比较容易调用。

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.

如果您有兴趣基于C ++的API系统,你可能有一个看看俳句;就个人而言,这是一个方面,因为这些我在这个项目很感兴趣。

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.

顺便说一句,如果你打算做Win32编程只是API,您最好找一本好书来适应这些特殊性和其他Win32的成语。两个著名的是在校长,新人和的 Petzhold

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.

这篇关于Win32应用程序不那么面向对象的,为什么有这么多的三分球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆